# Looping for storing differet matrix in different rows but in same matrix.

1 view (last 30 days)
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.

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};

#### 1 Comment

Darpan Verma on 1 Dec 2019
Works like a cake. Thanks mate.

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

Vladimir Sovkov on 1 Dec 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 on 1 Dec 2019
Yes, my apologies
Darpan Verma on 1 Dec 2019