Merging Smaller Cells into a Bigger one

2 views (last 30 days)
Dear Community,
I am currently facing one problem:
I have two cell arrays, one cell is (98,2) and the other the other is (128,2), each variable consists of a 3x3 matrix and I want to insert this packages of information in the diagonal of a cell array of (226,226). For that I've prepared the following loops:
K_reinforcement = cell(98,2);
for iik=1:2:97
K_reinforcement{iik,1} = - pinv(New_Gesamtsystem_reinforcement{iik,2}) * New_Gesamtsystem_reinforcement{iik,1};
K_reinforcement{iik,2} = pinv(New_Gesamtsystem_reinforcement{iik,2});
end
for iik=2:2:98
K_reinforcement{iik,1} = transpose(pinv(New_Gesamtsystem_reinforcement{iik-1,2}));
K_reinforcement{iik,2} = (New_Gesamtsystem_reinforcement{iik,2}) * pinv(New_Gesamtsystem_reinforcement{iik-1,2});
end
Displacement_Total_System = cell(226,226);
for mkm = 1:2:97
for mkn = 1:2:97
Displacement_Total_System{mkm,mkm} = K_tank_reinforcement{mkm,1};
Displacement_Total_System{mkm,mkm+1} = K_tank_reinforcement{mkm,2};
end
end
for mkm = 2:2:98
for mkn = 2:2:98
Displacement_Total_System{mkm,mkm} = K_tank_reinforcement{mkm,1};
Displacement_Total_System{mkm,mkm+1} = K_tank_reinforcement{mkm,2};
end
end
for mkm = 97:2:225
for mkn = 97:2:225
Displacement_Total_System{mkm,mkm} = K_spherical_dome{mkm,1};
Displacement_Total_System{mkm,mkm+1} = K_spherical_dome{mkm,2};
end
end
for mkm = 98:2:226
for mkn = 98:2:226
Displacement_Total_System{mkm,mkm} = K_spherical_dome{mkm,1};
Displacement_Total_System{mkm,mkm+1} = K_spherical_dome{mkm,2};
end
end
Unfortunately, I get the following error: Index in position 1 exceeds array bounds (must not exceed 128).
Do you have any solution or recommendation to this problem?
Thank you!
  2 Comments
David Hill
David Hill on 21 Sep 2020
Is your data all of the same type (double)?
Marcelo Boldt
Marcelo Boldt on 21 Sep 2020
yes my data is same type (double)

Sign in to comment.

Accepted Answer

Rik
Rik on 21 Sep 2020
Edited: Rik on 21 Sep 2020
The source of your problem is that you are using the same indices for your big array as for your smaller array.
The solution to your problem is to skip this entire business of nested loops (where you don't even use the inner loop variable). Use eye instead to find the indices to store everything, or calculate the indices yourself.
K_tank_reinforcement=cell(98,2);
K_spherical_dome=cell(128,2);
%fill with random data
for n=1:numel(K_tank_reinforcement)
K_tank_reinforcement{n}=rand;
end
for n=1:numel(K_spherical_dome)
K_spherical_dome{n}=rand;
end
%merge to a single cell array
K={K_tank_reinforcement,K_spherical_dome};
sz=cellfun('size',K,1);
Displacement_Total_System = cell(sum(sz)+[0 1]);%account for extra column
for n=1:numel(K)
main_diagonal=1:sz(n);
main_diagonal=main_diagonal+sum(sz(1:(n-1)));
ind=sub2ind(size(Displacement_Total_System),main_diagonal,main_diagonal);
Displacement_Total_System(ind)=K{n}(:,1);
ind=sub2ind(size(Displacement_Total_System),main_diagonal,main_diagonal+1);
Displacement_Total_System(ind)=K{n}(:,2);
end
%complicated way (which you were trying to do):
K_tank_reinforcement=cell(98,2);
K_spherical_dome=cell(128,2);
%fill with random data
for n=1:numel(K_tank_reinforcement)
K_tank_reinforcement{n}=rand;
end
for n=1:numel(K_spherical_dome)
K_spherical_dome{n}=rand;
end
%merge to a single cell array
K={K_tank_reinforcement,K_spherical_dome};
sz=cellfun('size',K,1);
Displacement_Total_System = cell(sum(sz)+[0 1]);%account for extra column
K_ind=arrayfun(@(n)[1:sz(n);n*ones(1,sz(n))],1:numel(sz),'Uni',0);
K_ind=cell2mat(K_ind);
for mkm=1:(sum(sz))
mkn=(1:2:sz(K_ind(2,mkm)))+(1-mod(mkm,2));
for mkn=mkn%you aren't using mkn anywhere, so you could also do mkn=mkn(end)
tmp=K{K_ind(2,mkm)};%either K_tank_reinforcement or K_spherical_dome
Displacement_Total_System{mkm,mkm }=tmp{K_ind(1,mkm),1};
Displacement_Total_System{mkm,mkm+1}=tmp{K_ind(1,mkm),2};
end
end
  9 Comments
Rik
Rik on 22 Sep 2020
Do this
save('variables_for_Matlab_Answers.mat','K_tank_reinforcement','K_spherical_dome')
and attach the resulting file. You're making this much harder than it needs to be. None of your mat files contain the relevant variables and you m file fails at the third line (after the totally useless clear all, which you should replace by clear).
It isn't relevant at all for the question you were asking what your variables mean, only their shape and data type. You want to put the data from two x2 cell arrays along a diagonal. I provided code that will do that. You claim it doesn't work for your data because it has a different shape. You need to either give me similar variables so I can reproduce the problem, or explain in detail what error you're getting (or how the result differs from the required result).
Marcelo Boldt
Marcelo Boldt on 22 Sep 2020
Thank you Rik for your patience, I tried it out and it worked

Sign in to comment.

More Answers (0)

Categories

Find more on Data Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!