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

To create a 5 by 3 matrix that starts at 1, increments by 1, and then doubles each column, you can use the following code:

main.m
A = ones(5,3);
for i = 1:3
    A(:,i) = A(:,i) * 2^(i-1);
end
62 chars
5 lines

The ones function creates a 5 by 3 matrix filled with ones. The for loop then iterates over each column of A, and multiplies each element in that column by 2^(i-1), where i is the current column index. The ^(i-1) part means we are doubling the column for each iteration of the loop.

The resulting matrix A will look like this:

main.m
A =

     1     1     1
     2     2     2
     4     4     4
     8     8     8
    16    16    16
100 chars
8 lines

gistlibby LogSnag