create a for loop that makes a 5 by 3 matrix, that starts 1-2, then doubles each column in matlab in matlab

main.m
% Initializing the matrix with zeros
M = zeros(5,3);

% Using nested for loop to initialize and double the values in each column
for i = 1:3
    % Initializing the first element in column i
    M(1,i) = i;

    % Using another for loop to double the element in column i until row 5
    for j = 2:5
        M(j,i) = M(j-1,i) * 2;
    end
end

% Displaying the resulting matrix
disp(M);
385 chars
17 lines

This code first initializes a 5 by 3 matrix M with zeros. Then it uses a nested for loop to initialize and double the values in each column. The outer for loop iterates over the columns from 1 to 3, and the inner for loop iterates over the rows from 2 to 5.

In each iteration of the outer loop, the first element in the current column is initialized to the current column number (i.e. 1, 2 or 3). Then in each iteration of the inner loop, the current element in the current column is set to be twice the previous element in that column.

Finally, the resulting matrix M is displayed using the disp function.

gistlibby LogSnag