Constructing a patterned matrix

Hello all,
I'm a new matlab user, and I have a question on constructing a matrix that has a set pattern.
For example, say I want to make a 4x4 matrix A whose first column entries are all ones. Entries for row 1 columns 2 to 4 are 2, 3, and 4, respectively. For all columns 2 to 4, each time we go down one row, the entries are raised to the power of 2 one time.
So, the entries in column 2 would be 2, 4, 8, and 16.
Instead of entering the entries to the matrix one at a time, I'm wondering if there's a command that I can use to setup the matrix.
The actual matrix that I need to construct is actually much bigger than that, so it would take me awhile to enter the entries 1 by 1.
Thanks for any help!

Answers (1)

n = 4;
v = 1:n;
bsxfun(@power, v,v.')

2 Comments

Can you explain what those commands do?
Im a complete novice to matlab.
They construct the matrix you asked:
1:n --> [1 2 3 4];
bsxfun(@power,v,v.') --> [1 2 3 4] to the power of [1; 2; 3; 4]. Rise v to 1 for the first row, to 2 for the second, etc...

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

Jay
on 29 May 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!