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!
回答(1 个)
Oleg Komarov
2011-5-29
n = 4;
v = 1:n;
bsxfun(@power, v,v.')
2 个评论
Jay
2011-5-29
Oleg Komarov
2011-5-29
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...
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!