reference elements in a matrix based on values in a vector

I have the 1x9 vector [5 3 2 6 7 26 4 33 3]
I want to obtain the matrix [5 3 2; 6 7 26; 4 33 3] using completely vectorized code. How can this be done?
As a start, I have vectorized code to build the 3x3 matrix [ 1 2 3; 4 5 6; 7 8 9], so that the values are the vector columns, but I don't know how to do the vectorized referencing.
Thanks

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 23 日
編集済み: Azzi Abdelmalek 2014 年 2 月 23 日

0 投票

A= [5 3 2 6 7 26 4 33 3]
out= reshape(A,3,[])'

4 件のコメント

Christopher
Christopher 2014 年 2 月 23 日
This works, but can it be made more general so that the 3x3 matrix can have any numbers in it?
For instance, if the matrix were [2 5 9; 3 2 1; 5 7 3] the solution would be [3 7 3; 2 3 5; 7 4 2]
Image Analyst
Image Analyst 2014 年 2 月 23 日
編集済み: Image Analyst 2014 年 2 月 23 日
There is nothing in "out= reshape(A,3,[])'" that depends on what numbers are in A. Your "A" can be anything - any numbers at all.
So now you have 2D arrays rather than a row vector. But what I don't understand is how you got that solution. Why are there 2 7's in the solution but only one in the "matrix"? Please explain element by element how you got the solution element from the input matrix.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 23 日
How?
Christopher
Christopher 2014 年 2 月 23 日
編集済み: Christopher 2014 年 2 月 23 日
There are two 7's because there are two 5's. The 5th element in the vector is =7. The numbers in the matrix should refer to the column number in the vector. You see?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

タグ

質問済み:

2014 年 2 月 23 日

編集済み:

2014 年 2 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by