Looping for storing differet matrix in different rows but in same matrix.
1 view (last 30 days)
Show older comments
Darpan Verma
on 1 Dec 2019
Commented: Darpan Verma
on 1 Dec 2019
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.
0 Comments
Accepted Answer
Vladimir Sovkov
on 1 Dec 2019
Before the loop, initiate
Calculated_Data=zeros(numel(alpha_data),numel(alpha_data{1,1}));
k=0;
within the loop, use
k=k+1;
Calculated_Data(k,:) = alpha_data{i,j};
More Answers (1)
Bhaskar R
on 1 Dec 2019
Edited: Bhaskar R
on 1 Dec 2019
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 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!