How to store matrices elements to cell array?

I have 3 four dimensional matrices having same size. I need to create a cell array with same dimension and size of matrices and need to store same position element in each matrices to corresponding single element in the cell array. ie for example, if 'C' is the cell and 'M1', 'M2' and 'M3' are the matrices then c{1} should be [M1(1), M2(1), M3(1)] and c{2} should be [M1(2), M2(2), M3(2)] like that have to fill every elements in multidimensional cell array. Can I do this without using loop?
How it is possible.

回答 (1 件)

Stephen23
Stephen23 2016 年 2 月 3 日
編集済み: Stephen23 2016 年 2 月 3 日

0 投票

This is trivial using num2cell, once the arrays are concatenated together (see below):
>> M1 = rand(3,5,2,4);
>> M2 = rand(3,5,2,4);
>> M3 = rand(3,5,2,4);
>> C = num2cell(cat(5,M1,M2,M3),5);
Optional, convert vectors into columns:
>> C = cellfun(@squeeze,C,'UniformOutput',false);
Note that this whole task would have been simpler if the data was stored in one numeric array anyway. The first step of my answer is to concatenate the three arrays into one array: this should really be how the data was stored, then the answer is trivial. Numbered variables are a sign of bad programming, and lead to beginners write all sorts of poor code:
Summary: numbered variables are a de facto index. So turn them into real indices!

カテゴリ

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

質問済み:

2016 年 2 月 3 日

編集済み:

2016 年 2 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by