merge parts of arrays of cell array into matrix using loop

5 ビュー (過去 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.
  2 件のコメント
Marko
Marko 2019 年 12 月 3 日
Thank you! That's it!
Luna
Luna 2019 年 12 月 3 日
Your welcome :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by