Vector Manipulation into a matrix?

1 回表示 (過去 30 日間)
Daniel Tanner
Daniel Tanner 2020 年 3 月 13 日
コメント済み: Daniel Tanner 2020 年 3 月 13 日
I have a vector, A, of dimensions 21-by-1. How would I be able to manipulate this matrix such that:
A = [a;b;c;d;e;f;g;h;i;...]
Becomes a 3-by-7 matrix, B:
B = [c,b,a;f,e,d;i,h,g,...]
Any help would be much appreciated! Thanks.

採用された回答

Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 13 日
Hi Daniel,
This can be solved by first using the reshape function and then performing the circular shift operation.
% Suppose the vector is a - of length 21
a = rand(21,1); % random column vector 21-by-1
% Now use the reshape operation to the desired size
aReshape = reshape(a,3,7); % Size is 3-by-7
% Store the first row in a variable
temp = aReshape(1,:);
% Replace the first row with the third row
aReshape(1,:) = aReshape(3,:);
% Replace the third row with the first row
aReshape(3,:) = temp;
out = aReshape;
The documentation link for the reshape function is
Hope this helps.
Regards,
Sriram
  1 件のコメント
Daniel Tanner
Daniel Tanner 2020 年 3 月 13 日
Thank you Sriram, very informative answer and much more understandable now!

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

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2020 年 3 月 13 日
A=(1:21)';
temp1=reshape(A,3,[]);
temp2=temp1';
B=fliplr(temp2)
  1 件のコメント
Daniel Tanner
Daniel Tanner 2020 年 3 月 13 日
Thank you!

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by