Merging Smaller Cells into a Bigger one

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 comentarios

David Hill
David Hill el 21 de Sept. de 2020
Is your data all of the same type (double)?
Marcelo Boldt
Marcelo Boldt el 21 de Sept. de 2020
yes my data is same type (double)

Iniciar sesión para comentar.

 Respuesta aceptada

Rik
Rik el 21 de Sept. de 2020
Editada: Rik el 21 de Sept. de 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 comentarios

Marcelo Boldt
Marcelo Boldt el 21 de Sept. de 2020
Hi Rik, your solution approach is good, but I have a 3x3 Matrix, how would you do in that case?
Rik
Rik el 21 de Sept. de 2020
If I use the code below to generate the input my code still works as expected.
for n=1:numel(K_tank_reinforcement)
K_tank_reinforcement{n}=rand(3);
end
for n=1:numel(K_spherical_dome)
K_spherical_dome{n}=rand(3);
end
Please write code to generate random data, or attach your actual data so we can reproduce your issue.
Marcelo Boldt
Marcelo Boldt el 21 de Sept. de 2020
I updated it. My bad, what I was trying to say is that each variable contains of the cell array contains a 3x3 matrix inside. Therefore, the right dimension of the cells would be 128x2(3x3) and 98x2(3x3)
Rik
Rik el 21 de Sept. de 2020
Please write code or attach data. You can see in my example that it doesn't have to be fancy. You clearly know what data you have, but your description is not clear.
Marcelo Boldt
Marcelo Boldt el 21 de Sept. de 2020
Editada: Rik el 22 de Sept. de 2020
This is my updated version
New_Gesamtsystem_Zylinder = mat2cell(cell2mat(Gesamtsystem_zylinder), 3 * ones(1,98), 3 * ones(1,2));
K_tank_reinforcement = cell(98,2);
for ii=1:2:97
K_tank_reinforcement{ii,1} = - pinv(New_Gesamtsystem_Zylinder{ii,2}) * New_Gesamtsystem_Zylinder{ii,1};
K_tank_reinforcement{ii,2} = pinv(New_Gesamtsystem_Zylinder{ii,2});
end
for ii=2:2:98
K_tank_reinforcement{ii,1} = transpose(pinv(New_Gesamtsystem_Zylinder{ii-1,2}));
K_tank_reinforcement{ii,2} = (New_Gesamtsystem_Zylinder{ii,2}) * pinv(New_Gesamtsystem_Zylinder{ii-1,2});
end
%% Runge Kutta 4th Order - Tank - Spherical Dome
A_sp = cell(65,1);
for jj = 1:L_sp
A_sp{jj,:} = Differential_gleichungssystem_Kugel.Differentialmatrix_Kugel{jj,:};
end
%Runge Kutta 4
for j=1:1:(L_sp-1) % calculation loop
m_1 = A_sp{j,:}*B_sp{:,j}; % calculating coefficient
m_2 = (A_sp{j,:}+(A_sp{j+1,:}- A_sp{j,:})/2)*(B_sp{:,j}+0.5*s*m_1); %(A_sp{j,:}+(A_sp{j+1,:}- A_sp{j,:})/2) for replacement
m_3 = (A_sp{j,:}+(A_sp{j+1,:}- A_sp{j,:})/2)*(B_sp{:,j}+0.5*s*m_2);
m_4 = (A_sp{j+1,:})*(B_sp{:,j}+m_3*s);
B_sp{:,j+1} = B_sp{:,j} + (s/6)*(m_1+2*m_2+2*m_3+m_4); % main equation
Ubertragungsmatrix_sp{j,:} = B_sp{:,j};
end
Ubertragungsmatrix_sp = Ubertragungsmatrix_sp(1:end-1,:);
Gesamtsystem_KUGEL = cell(64,1);
for t=1:64
Gesamtsystem_KUGEL{t,:} = Ubertragungsmatrix_sp{t,:}*Transformation_Matrix{t,:};
end
%% Die Elementsteifigkeitsmatrix - Spherical Dome
% Weggrößenmethode: Unbekannte sind Verschiebungen.
New_Gesamtsystem_Kugel = mat2cell(cell2mat(Gesamtsystem_KUGEL), 3 * ones(1,128), 3 * ones(1,2));
% Funciona, pero hay que corroborar los numeros
K_spherical_dome = cell(128,2);
for ii=1:2:127
K_spherical_dome{ii,1} = - pinv(New_Gesamtsystem_Kugel{ii,2}) * New_Gesamtsystem_Kugel{ii,1};
K_spherical_dome{ii,2} = pinv(New_Gesamtsystem_Kugel{ii,2});
end
for ii=2:2:128
K_spherical_dome{ii,1} = transpose(pinv(New_Gesamtsystem_Kugel{ii-1,2}));
K_spherical_dome{ii,2} = (New_Gesamtsystem_Kugel{ii,2}) * pinv(New_Gesamtsystem_Kugel{ii-1,2});
end
%% General Conditions - Cylinder - Reinforcement
ax = 2300/49; % step size
x = 0:ax:2300;
L = length(x);
B_reinforcement = cell(1,50); % initialzation
B_reinforcement{:,1} = eye(6,6);
Ubertragungsmatrix_reinforcement = cell(length(x),1);
A_reinforcement = cell(50,1);
Einheitsmatrix = eye(6,6);
%% Runge Kutta 4th order - Tank + Reinforcement Cylindrical Part
% Hay que calcular la 3er abd matrix primero
for gg = 1:50
A_reinforcement{gg,:} = Differetial_gleichung_system_Zylinder.Differentialmatrix_Zylinder{gg,:};
end
for iii=1:(L-1) % calculation loop
n_1 = A_reinforcement{iii,:} * B_reinforcement{:,iii}; % calculating coefficient
n_2 = (A_reinforcement{iii,:} + (A_reinforcement{iii+1,:}-A_reinforcement{iii,:})/2) * (B_reinforcement{:,iii}+0.5*ax*n_1);
n_3 = (A_reinforcement{iii,:} + (A_reinforcement{iii+1,:}-A_reinforcement{iii,:})/2) * (B_reinforcement{:,iii}+0.5*ax*n_2);
n_4 = (A_reinforcement{iii+1,:}) * (B_reinforcement{:,iii}+n_3*ax);
B_reinforcement{:,iii+1} = B_reinforcement{:,iii} + (ax/6)*(n_1+2*n_2+2*n_3+n_4); % main equation
Ubertragungsmatrix_reinforcement{iii,:} = B_reinforcement{:,iii};
end
Gesamtsystem_reinforcement = cell(49,1);
for iki = 1:49
Gesamtsystem_reinforcement{iki,:} = Ubertragungsmatrix_z{iki,:};
end
%% Die Elementsteifigkeitsmatrix - Tank + Reinforcement - Cylindrical Part
New_Gesamtsystem_reinforcement = mat2cell(cell2mat(Gesamtsystem_reinforcement), 3 * ones(1,98), 3 * ones(1,2));
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
%% Tank System Displacement Method -
Displacement_Total_System = cell(226,226);
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
%%
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
Rik
Rik el 21 de Sept. de 2020
Editada: Rik el 21 de Sept. de 2020
This isn't at all compact and I'm still missing variables to run this. Also, please take the 2 seconds it takes to click the code button when posting a comment. This is not making me want to help you.
If you don't understand Matlab enough to write the generator code (or can't be bothered to): just attach your data in a mat file. Otherwise write at most 10 lines of code that will create the variables of the correct type and size.
Marcelo Boldt
Marcelo Boldt el 22 de Sept. de 2020
I am very sorry for this mistake, wasn't my intention. I just uploaded the mat files that I have been using. Only the Runge kutta.mat file is a script the rest are variables for the script to run.
Rik
Rik el 22 de Sept. de 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 el 22 de Sept. de 2020
Thank you Rik for your patience, I tried it out and it worked

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 21 de Sept. de 2020

Comentada:

el 22 de Sept. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by