Azzera filtri
Azzera filtri

merge parts of arrays of cell array into matrix using loop

2 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Luna
Luna il 2 Dic 2019
Modificato: Luna il 2 Dic 2019
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.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by