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!

Respuestas (1)

Oleg Komarov
Oleg Komarov el 29 de Mayo de 2011

0 votos

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

2 comentarios

Jay
Jay el 29 de Mayo de 2011
Can you explain what those commands do?
Im a complete novice to matlab.
Oleg Komarov
Oleg Komarov el 29 de Mayo de 2011
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...

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

Jay
el 29 de Mayo de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by