create a for loop where in a matrix all other elements are three time the value above in matlab

To create a for loop where in a matrix all other elements are three times the value above, we can use indexing and element-wise multiplication in Matlab.

Here's an example code snippet that demonstrates this:

main.m
% Start with a 4x4 matrix of ones
A = ones(4);

% Loop through all the rows starting from the second row
for i = 2:size(A,1)
    % Loop through all the columns
    for j = 1:size(A,2)
        % Set the element value to three times the value above it
        A(i,j) = 3*A(i-1,j);
    end
end

% Display the resulting matrix
disp(A)
331 chars
15 lines

In this example, we first create a 4x4 matrix of ones using the ones function. Then, we loop through all the rows (starting from the second row) and columns of the matrix using two for loops. For each element in the matrix (excluding the first row), we set the value to three times the value of the element directly above it using indexing and element-wise multiplication. Finally, we display the resulting matrix using the disp function.

gistlibby LogSnag