merge parts of arrays of cell array into matrix using loop

조회 수: 1 (최근 30일)
Marko
Marko 2019년 12월 2일
댓글: Luna 2019년 12월 3일
Hi guys!
I have a cell array:
solution = 1×6 cell array
Columns 1 through 6
{6×300 double} {6×300 double} {6×300 double} {6×300 double} {6×300 double} {6×300 double}
Later it will be a cell arrays of 1 x 14000
I need to find a way to merge the first 20 elements (Ns*Nz) of the first row of each cell array.
This is what I basicly need:
sol = solution;
cC0_ges = [sol{1,1}(1,1:Nz*Ns); sol{1,2}(1,1:Nz*Ns); sol{1,3}(1,1:Nz*Ns); sol{1,4}(1,1:Nz*Ns); sol{1,5}(1,1:Nz*Ns); sol{1,6}(1,1:Nz*Ns)];
cC1_ges = [sol{1,1}(1,Nz*Ns+1:2*Nz*Ns); sol{1,2}(1,Nz*Ns+1:2*Nz*Ns); sol{1,3}(1,1:Nz*Ns); sol{1,4}(1,1:Nz*Ns+1:2*Nz*Ns); sol{1,5}(1,1:Nz*Ns+1:2*Nz*Ns); sol{1,6}(1,1:Nz*Ns+1:2*Nz*Ns)];
And so on - but I cant do this manually for 14000 times.
So what I tried is:
for j = 0:5
j = j + 1;
cC0_ges = solution{1,j}(1,1:Nz*Ns); %sol{1,2}(1,1:Nz*Ns); sol{1,3}(1,1:Nz*Ns); sol{1,4}(1,1:Nz*Ns); sol{1,5}(1,1:Nz*Ns); sol{1,6}(1,1:Nz*Ns)];
end
And
cC0_ges = [solution{1,:}(1,1:Nz*Ns)]'
But apparently it is not right.
  댓글 수: 2
Luna
Luna 2019년 12월 2일
What is Ns and Nz ?
Marko
Marko 2019년 12월 2일
Sorry, just connstant.
Nz = 20;
Ns = 1;

댓글을 달려면 로그인하십시오.

채택된 답변

Luna
Luna 2019년 12월 2일
편집: Luna 2019년 12월 2일
Try this:
solution = repmat({rand(6,300)},1,6);
cC0_ges = reshape(cell2mat(cellfun(@(x) x(1,1:20), solution,'uni',false)),6,20);
cC1_ges = reshape(cell2mat(cellfun(@(x) x(1,21:40), solution,'uni',false)),6,20);
cC2_ges = reshape(cell2mat(cellfun(@(x) x(1,41:60), solution,'uni',false)),6,20);
.
..
..
cC15_ges = reshape(cell2mat(cellfun(@(x) x(1,281:300), solution,'uni',false)),6,20);
%% OR
%% what you need from 1 to 20, 21 to 40, ... etc. in a for loop:
solution = repmat({rand(6,300)},1,6);
breakpoints1 = circshift([1:20:300,300],1);
breakpoints2 = 0:20:300;
breakpoints1(1) = [];
breakpoints2(1) = [];
breakpointsMatrix = [breakpoints1;breakpoints2]';
for i = 1:numel(breakpoints2)
cC_ges{i,1} = reshape(cell2mat(cellfun(@(x) x(1,breakpointsMatrix(i,1):breakpointsMatrix(i,2)), solution,'uni',false)),6,20);
end
You will get a 15x1 cell array each contains 6x20 doubles.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by