Manipulate multidimensional matrix in for loop

8 ビュー (過去 30 日間)
Anna
Anna 2017 年 5 月 3 日
コメント済み: Andrei Bobrov 2017 年 5 月 3 日
Hi, I have a matrix called multidim with 2x100x3 values. I would like to make this a 6x100 matrix by adding the matrix defined in each page under the previous one.
for i = 1:2
matrix = [multidim(:,:,i);multidim(:,:,i+1)];
end
This is what I have tried, but this only works if I have a 2x100x2 matrix. How do I make this a loop which remembers the previous created matrix and uses this in order to keep adding rows from the next page?

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 5 月 3 日
a = num2cell(multidim,[1,2]);
matrix = cat(1,a{:});
  2 件のコメント
Anna
Anna 2017 年 5 月 3 日
This works! Thank you so much!
Stephen23
Stephen23 2017 年 5 月 3 日
編集済み: Stephen23 2017 年 5 月 3 日
Note my answer is just one line, does not waste memory with unnecessary variables in the workspace, and runs around twice as fast as this answer:
Elapsed time is 6.32136 seconds. % this answer
Elapsed time is 3.98123 seconds. % my answer

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

その他の回答 (1 件)

Stephen23
Stephen23 2017 年 5 月 3 日
編集済み: Stephen23 2017 年 5 月 3 日
Use permute and reshape:
>> X(:,:,1) = [0,1,2;3,4,5];
>> X(:,:,2) = [6,7,8;9,10,11];
>> reshape(permute(X,[1,3,2]),[],3)
ans =
0 1 2
3 4 5
6 7 8
9 10 11

カテゴリ

Help Center および 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