Convert a vector into a matrix (row wise)

87 ビュー (過去 30 日間)
Amirhossein Moosavi
Amirhossein Moosavi 2020 年 7 月 2 日
編集済み: Tommy 2020 年 7 月 2 日
Let us assume vector A as follows:
A = [1 2 3 4; 5 6 7 8; 9 10 11 12];
I create two vectors (row wise) from this matrix as follows:
CP = randi([1 numel(A)]-1);
B = transpose(A);
C = B(1:CP);
D = B(CP+1:end);
Now, when I use reshpe function to construct a matrix as the same as A, the result is something else.
reshape([C D], 3, 4)
It seems that I need to make reshape to construct the matrix in row wise. Could you please help me?
  1 件のコメント
James Tursa
James Tursa 2020 年 7 月 2 日
Can you explain what you want to get as output? I.e., show us the actual numeric output you want.

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

採用された回答

Tommy
Tommy 2020 年 7 月 2 日
編集済み: Tommy 2020 年 7 月 2 日
From the reshape() docs, for the syntax
B = reshape(A,sz)
"The elements in B preserve their columnwise ordering from A."
If you want to "reshape" a vector so that the output matrix is filled along the rows first, you could reverse the requested dimensions in your call to reshape() and then transpose the result:
A2 = reshape([C D], 4, 3)';
I believe this could be generalized to any number of dimensions with a combination of reshape() and permute().

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by