How to store matrices elements to cell array?
Mostra commenti meno recenti
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.
Risposte (1)
>> 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!
Categorie
Scopri di più su Creating and Concatenating Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!