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

1 次查看(过去 30 天)
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
Vladimir Sovkov 2019-12-1
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 个)

Bhaskar R
Bhaskar R 2019-12-1
编辑:Bhaskar R 2019-12-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 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by