Looping for storing differet matrix in different rows but in same matrix.
Afficher commentaires plus anciens
Hello everyone,
I have got a 3x2 cell "alpha_data" in which each cell consists of 1x79 matrixs in them (shown below Fig2). I want to get all these 1x79 matrixes in one matrix "Calculated_Data", so as to have a final matrix of 6x79. i.e. All data into one matrix with all individuals cell matrixs goes into one matrix different rows. I wrote this code but its only giving me one alpha_data{3,2} (Fig 1)data into "Calculated_Data" matrix
for i = 1:size(alpha_data,1) %size(alpha_data,1) = 3
for j = 1:size(alpha_data,2) %size(alpha_data,1) = 2
Calculated_Data = vertcat(alpha_data{i,j});
end
end
The above code that I wrote is only giving one cell's data into final matrix.
Fig1. "Calculated_Data"only giving me one alpha_data{3,2} , but I need 6x79 (all data in the same matrix)

Fig 2. alpha_data: A 3x2 cell, each cell have 1x79 matrix

Any helps would be greately appreacited.
Réponse acceptée
Plus de réponses (1)
In simple way
Calculated_Data = reshape([alpha_data{:}],6,79);
In for loop
Calculated_Data = zeros(6, 79);
k = 1; % counter variable
for i = 1:size(alpha_data,1) %size(alpha_data,1) = 3
for j = 1:size(alpha_data,2) %size(alpha_data,1) = 2
Calculated_Data(k,:) = alpha_data{i,j};
k = k+1;
end
end
3 commentaires
Vladimir Sovkov
le 1 Déc 2019
The "simple" version looks OK.
The "loop" version will not work as both alpha_data{i,j} and alpha_data{j,i} try to occupy the same row of Calculated_Data.
Bhaskar R
le 1 Déc 2019
Yes, my apologies
Darpan Verma
le 1 Déc 2019
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!