create a for loop that makes a 5 by 3 matrix then doubles each column in matlab in matlab in matlab

main.m
% Define size of matrix
rows = 5;
cols = 3;

% Create matrix filled with zeros
matrix = zeros(rows, cols);

% Fill the matrix with values
for i = 1:cols
    matrix(:, i) = i;
end

% Double each column
matrix = matrix .* 2;
223 chars
15 lines

Explanation:

  • The first two lines define the size of the matrix we want to create.
  • The zeros function creates a matrix filled with zeros.
  • The for loop fills each column of the matrix with values ranging from 1 to the number of columns.
  • The last line doubles each element in the matrix. The .* operator performs element-wise multiplication.

gistlibby LogSnag