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!