merge parts of arrays of cell array into matrix using loop

1 vue (au cours des 30 derniers jours)
Marko
Marko le 2 Déc 2019
Commenté : Luna le 3 Déc 2019
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 commentaires
Luna
Luna le 2 Déc 2019
What is Ns and Nz ?
Marko
Marko le 2 Déc 2019
Sorry, just connstant.
Nz = 20;
Ns = 1;

Connectez-vous pour commenter.

Réponse acceptée

Luna
Luna le 2 Déc 2019
Modifié(e) : Luna le 2 Déc 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.
  2 commentaires
Marko
Marko le 3 Déc 2019
Thank you! That's it!
Luna
Luna le 3 Déc 2019
Your welcome :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by