Concatenate vector in a 3d matrix

Hi. I have these vectors: a = [11 12 13 14]; b = [21 22 23 24]; c = [31 32 33 34]; d = [41 42 43 44];
I want to concatenate them in a 2x2x4 matrix: M(:,:,1) = [11 21; 31 41]; M(:,:,2)= [12 22; 32 42]; and so on.
How can I do that?

 採用された回答

James Tursa
James Tursa 2012 年 4 月 13 日

1 投票

For your exact problem:
M = reshape([a;c;b;d],2,2,4);
Then generalize as appropriate. E.g., if a,b,c,d are not row vectors to start with,
M = reshape([a(:)';c(:)';b(:)';d(:)'],2,2,4);
etc.

4 件のコメント

Alessandro Masullo
Alessandro Masullo 2012 年 4 月 13 日
Perfect! Thank you!
But now I have another problem...
I have two matrices
M = (3 by 3 by 50)
V = (3 by 1 by 50)
The third dimension is the time.
I want the result of M*V, as a product (3 by 3) * (3 by 1) for each time step.
Neither * nor .* has worked :(
Thomas
Thomas 2012 年 4 月 13 日
Will this work?
for i=1:50
pr(:,:,i)=M(:,:,i)*V(:,:,i);
end
Alessandro Masullo
Alessandro Masullo 2012 年 4 月 13 日
I would like to avoid cycles... :)
James Tursa
James Tursa 2012 年 4 月 13 日
MtimesV = mtimesx(M,V,'speed');
You can find mtimesx on the FEX here:
http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by