Loop to populate a 12 by 12 matrix

3 views (last 30 days)
Hello Guys,
I am tryling to form a 12 by 12 matrix for 11 elements. I am getting some errors. Take a look at my code below: it looks perfect to me but it is not working
SC1=rand(11,1);% Test Data
SC2=rand(11,1);
SC3=rand(11,1);
SC4=rand(11,1);
SC5=rand(11,1);
SC6=rand(11,1);
SC7=rand(11,1);
SC8=rand(11,1);
n=size(SC8,1);
K1=zeros(12,1);
K2=zeros(12,1);
K3=zeros(12,1);
K4=zeros(12,1);
K5=zeros(12,1);
K6=zeros(12,1);
K7=zeros(12,1);
K8=zeros(12,1);
K9=zeros(12,1);
K10=zeros(12,1);
K11=zeros(12,1);
K12=zeros(12,1);
K=zeros(12,12);
for i=1:n
K1(i)=[SC1(i);0;0;0;0;0;-SC1(i);0;0;0;0;0];
K2(i)=[0;SC6(i);0;0;0;SC2(i);0;-SC6(i);0;0;0;SC2(i)];
K3(i)=[0;0;SC7(i);0;-SC3(i);0;0;0;-SC7(i);0;-SC3(i);0];
K4(i)=[0;0;0;SC4(i);0;0;0;0;0;-SC4(i);0;0];
K5(i)=[0;0;-SC3(i);0;(2*SC5(i));0;0;0;SC3(i);0;SC5(i);0];
K6(i)=[0;SC2(i);0;0;0;(2*SC8(i));0;-SC2(i);0;0;0;SC8(i)];
K7(i)=-K1;
K8(i)=-K2;
K9(i)=-K3;
K10(i)=-K4;
K11(i)=[0;0;-SC3(i);0;SC5(i);0;0;0;SC3(i);0;(2*SC5(i));0];
K12(i)=[0;SC2(i);0;0;0;SC8(i);0;SC2(i);0;0;0;(2*SC8(i))];
K(i)=[K1(i) K2(i) K3(i) K4(i) K5(i) K6(i) K7(i) K8(i) K9(i) K10(i) K11(i) K12(i)];
end
  1 Comment
DARLINGTON ETAJE
DARLINGTON ETAJE on 24 Jun 2021
Thank you so much Walter. It worked. You are an excellent person. The only challenge is that working with the code in struct is different than in a double.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 24 Jun 2021
SC1=rand(11,1);% Test Data
SC2=rand(11,1);
SC3=rand(11,1);
SC4=rand(11,1);
SC5=rand(11,1);
SC6=rand(11,1);
SC7=rand(11,1);
SC8=rand(11,1);
n=size(SC8,1);
K1 = repmat({zeros(12,1)},n,1);
K2 = repmat({zeros(12,1)},n,1);
K3 = repmat({zeros(12,1)},n,1);
K4 = repmat({zeros(12,1)},n,1);
K5 = repmat({zeros(12,1)},n,1);
K6 = repmat({zeros(12,1)},n,1);
K7 = repmat({zeros(12,1)},n,1);
K8 = repmat({zeros(12,1)},n,1);
K9 = repmat({zeros(12,1)},n,1);
K10 = repmat({zeros(12,1)},n,1);
K11 = repmat({zeros(12,1)},n,1);
K12 = repmat({zeros(12,1)},n,1);
K = repmat({zeros(12,1)},n,1);
for i=1:n
K1{i} = [SC1(i);0;0;0;0;0;-SC1(i);0;0;0;0;0];
K2{i} = [0;SC6(i);0;0;0;SC2(i);0;-SC6(i);0;0;0;SC2(i)];
K3{i} = [0;0;SC7(i);0;-SC3(i);0;0;0;-SC7(i);0;-SC3(i);0];
K4{i} = [0;0;0;SC4(i);0;0;0;0;0;-SC4(i);0;0];
K5{i} = [0;0;-SC3(i);0;(2*SC5(i));0;0;0;SC3(i);0;SC5(i);0];
K6{i} = [0;SC2(i);0;0;0;(2*SC8(i));0;-SC2(i);0;0;0;SC8(i)];
K7{i} = -K1{i};
K8{i} = -K2{i};
K9{i} = -K3{i};
K10{i} = -K4{i};
K11{i} = [0;0;-SC3(i);0;SC5(i);0;0;0;SC3(i);0;(2*SC5(i));0];
K12{i} = [0;SC2(i);0;0;0;SC8(i);0;SC2(i);0;0;0;(2*SC8(i))];
K{i} = [K1(i); K2(i); K3(i); K4(i); K5(i); K6(i); K7(i); K8(i); K9(i); K10(i); K11(i); K12(i)];
end
K
K = 11×1 cell array
{12×1 cell} {12×1 cell} {12×1 cell} {12×1 cell} {12×1 cell} {12×1 cell} {12×1 cell} {12×1 cell} {12×1 cell} {12×1 cell} {12×1 cell}
K{1}
ans = 12×1 cell array
{12×1 double} {12×1 double} {12×1 double} {12×1 double} {12×1 double} {12×1 double} {12×1 double} {12×1 double} {12×1 double} {12×1 double} {12×1 double} {12×1 double}
K{1}{1}
ans = 12×1
0.3563 0 0 0 0 0 -0.3563 0 0 0

More Answers (0)

Community Treasure Hunt

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

Start Hunting!