MATLAB Answers

How to simplify my looping code?

1 view (last 30 days)
I have a looping. But its too long, How to simplify my Looping code? This is my code
clc;
clear all;
x=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.0];
y=[190 187 188 189 190 190 190 190 190 190 192 193 193 193 193 196 196 197 198 200 196 197 197 198 196 192 194 194 193 193 195 196 196 199 197 196 195 196 195 196 196 196 195 194 193 188 190 190 193 190 188 186 185 184 182 185 184 183 183 181 184 185 185 184 185 185 188 189 190 189 187 187 188 188 189 189 188 186 183 176 174 181 181 177 175 175 173 176 178 177 174 174 174 173 173 174 174 178 178 180];
n =length(x);
for i=1:n
xk2(i)=x(i)^2;
sumxk2=sum(xk2)
xk3(i)=x(i)^3;
sumxk3=sum(xk3);
xk4(i)=x(i)^4;
sumxk4=sum(xk4);
xk5(i)=x(i)^5;
sumxk5=sum(xk5);
xk6(i)=x(i)^6;
sumxk6=sum(xk6);
xk7(i)=x(i)^7;
sumxk7=sum(xk7);
xk8(i)=x(i)^8;
sumxk8=sum(xk8);
xk9(i)=x(i)^9;
sumxk9=sum(xk9);
xk10(i)=x(i)^10;
sumxk10=sum(xk10);
xk11(i)=x(i)^11;
sumxk11=sum(xk11);
xk12(i)=x(i)^12;
sumxk12=sum(xk12);
xk13(i)=x(i)^13;
sumxk13=sum(xk13);
xk14(i)=x(i)^14;
sumxk14=sum(xk14);
xk15(i)=x(i)^15;
sumxk15=sum(xk15);
xk16(i)=x(i)^16;
sumxk16=sum(xk16);
xk17(i)=x(i)^17;
sumxk17=sum(xk17);
xk18(i)=x(i)^18;
sumxk18=sum(xk18);
xk19(i)=x(i)^19;
sumxk19=sum(xk19);
xk20(i)=x(i)^20;
sumxk20=sum(xk20);
xk21(i)=x(i)^21;
sumxk21=sum(xk21);
xk22(i)=x(i)^22;
sumxk22=sum(xk22);
xk23(i)=x(i)^23;
sumxk23=sum(xk23);
xk24(i)=x(i)^24;
sumxk24=sum(xk24);
xk25(i)=x(i)^25;
sumxk25=sum(xk25);
xk26(i)=x(i)^26;
sumxk26=sum(xk26);
xk27(i)=x(i)^27;
sumxk27=sum(xk27);
xk28(i)=x(i)^28;
sumxk28=sum(xk28);
xk29(i)=x(i)^29;
sumxk29=sum(xk29);
xk30(i)=x(i)^30;
sumxk30=sum(xk30);
xk31(i)=x(i)^31;
sumxk31=sum(xk31);
xk32(i)=x(i)^32;
sumxk32=sum(xk32);
xk33(i)=x(i)^33;
sumxk33=sum(xk33);
xk34(i)=x(i)^34;
sumxk34=sum(xk34);
xk35(i)=x(i)^35;
sumxk35=sum(xk35);
xk36(i)=x(i)^36;
sumxk36=sum(xk36);
xk37(i)=x(i)^37;
sumxk37=sum(xk37);
xk38(i)=x(i)^38;
sumxk38=sum(xk38);
xk39(i)=x(i)^39;
sumxk39=sum(xk39);
xk40(i)=x(i)^40;
sumxk40=sum(xk40);
xk41(i)=x(i)^41;
sumxk41=sum(xk41);
xk42(i)=x(i)^42;
sumxk42=sum(xk42);
xk43(i)=x(i)^43;
sumxk43=sum(xk43);
xk44(i)=x(i)^44;
sumxk44=sum(xk44);
xk45(i)=x(i)^45;
sumxk45=sum(xk45);
xk46(i)=x(i)^46;
sumxk46=sum(xk46);
xk47(i)=x(i)^47;
sumxk47=sum(xk47);
xk48(i)=x(i)^48;
sumxk48=sum(xk48);
xk49(i)=x(i)^49;
sumxk49=sum(xk49);
xk50(i)=x(i)^50;
sumxk50=sum(xk50);
xk51(i)=x(i)^51;
sumxk51=sum(xk51);
xk52(i)=x(i)^52;
sumxk52=sum(xk52);
xk53(i)=x(i)^53;
sumxk53=sum(xk53);
xk54(i)=x(i)^54;
sumxk54=sum(xk54);
xk55(i)=x(i)^55;
sumxk55=sum(xk55);
xk56(i)=x(i)^56;
sumxk56=sum(xk56);
xk57(i)=x(i)^57;
sumxk57=sum(xk57);
xk58(i)=x(i)^58;
sumxk58=sum(xk58);
xk59(i)=x(i)^59;
sumxk59=sum(xk59);
xk60(i)=x(i)^60;
sumxk60=sum(xk60);
xk61(i)=x(i)^61;
sumxk61=sum(xk61);
xk62(i)=x(i)^62;
sumxk62=sum(xk62);
xk63(i)=x(i)^63;
sumxk63=sum(xk63);
xk64(i)=x(i)^64;
sumxk64=sum(xk64);
xk65(i)=x(i)^65;
sumxk65=sum(xk65);
xk66(i)=x(i)^66;
sumxk66=sum(xk66);
xk67(i)=x(i)^67;
sumxk67=sum(xk67);
xk68(i)=x(i)^68;
sumxk68=sum(xk68);
xk69(i)=x(i)^69;
sumxk69=sum(xk69);
xk70(i)=x(i)^70;
sumxk70=sum(xk70);
xk71(i)=x(i)^71;
sumxk71=sum(xk71);
xk72(i)=x(i)^72;
sumxk72=sum(xk72);
xk73(i)=x(i)^73;
sumxk73=sum(xk73);
xk74(i)=x(i)^74;
sumxk74=sum(xk74);
xk75(i)=x(i)^75;
sumxk75=sum(xk75);
xk76(i)=x(i)^76;
sumxk76=sum(xk76);
xk77(i)=x(i)^77;
sumxk77=sum(xk77);
xk78(i)=x(i)^78;
sumxk78=sum(xk78);
xk79(i)=x(i)^79;
sumxk79=sum(xk79);
xk80(i)=x(i)^80;
sumxk80=sum(xk80);
xk81(i)=x(i)^81;
sumxk81=sum(xk81);
xk82(i)=x(i)^82;
sumxk82=sum(xk82);
xk83(i)=x(i)^83;
sumxk83=sum(xk83);
xk84(i)=x(i)^84;
sumxk84=sum(xk84);
xk85(i)=x(i)^85;
sumxk85=sum(xk85);
xk86(i)=x(i)^86;
sumxk86=sum(xk86);
xk87(i)=x(i)^87;
sumxk87=sum(xk87);
xk88(i)=x(i)^88;
sumxk88=sum(xk88);
xk89(i)=x(i)^89;
sumxk89=sum(xk89);
xk90(i)=x(i)^90;
sumxk90=sum(xk90);
xk91(i)=x(i)^91;
sumxk91=sum(xk91);
xk92(i)=x(i)^92;
sumxk92=sum(xk92);
xk93(i)=x(i)^93;
sumxk93=sum(xk93);
xk94(i)=x(i)^94;
sumxk94=sum(xk94);
xk95(i)=x(i)^95;
sumxk95=sum(xk95);
xk96(i)=x(i)^96;
sumxk96=sum(xk96);
xk97(i)=x(i)^97;
sumxk97=sum(xk97);
xk98(i)=x(i)^98;
sumxk98=sum(xk98);
xk99(i)=x(i)^99;
sumxk99=sum(xk99);
xk100(i)=x(i)^100;
sumxk100=sum(xk100);
xk101(i)=x(i)^101;
sumxk101=sum(xk101);
xk102(i)=x(i)^102;
sumxk102=sum(xk102);
xk103(i)=x(i)^103;
sumxk103=sum(xk103);
xk104(i)=x(i)^104;
sumxk104=sum(xk104);
xk105(i)=x(i)^105;
sumxk105=sum(xk105);
xk106(i)=x(i)^106;
sumxk106=sum(xk106);
xk107(i)=x(i)^107;
sumxk107=sum(xk107);
xk108(i)=x(i)^108;
sumxk108=sum(xk108);
xk109(i)=x(i)^109;
sumxk109=sum(xk109);
xk110(i)=x(i)^110;
sumxk110=sum(xk110);
xk111(i)=x(i)^111;
sumxk111=sum(xk111);
xk112(i)=x(i)^112;
sumxk112=sum(xk112);
xk113(i)=x(i)^113;
sumxk113=sum(xk113);
xk114(i)=x(i)^114;
sumxk114=sum(xk114);
xk115(i)=x(i)^115;
sumxk115=sum(xk115);
xk116(i)=x(i)^116;
sumxk116=sum(xk116);
xk117(i)=x(i)^117;
sumxk117=sum(xk117);
xk118(i)=x(i)^118;
sumxk118=sum(xk118);
xk119(i)=x(i)^119;
sumxk119=sum(xk119);
xk120(i)=x(i)^120;
sumxk120=sum(xk120);
xk121(i)=x(i)^121;
sumxk121=sum(xk121);
xk122(i)=x(i)^122;
sumxk122=sum(xk122);
xk123(i)=x(i)^123;
sumxk123=sum(xk123);
xk124(i)=x(i)^124;
sumxk124=sum(xk124);
xk125(i)=x(i)^25;
sumxk125=sum(xk125);
xk126(i)=x(i)^126;
sumxk126=sum(xk126);
xk127(i)=x(i)^127;
sumxk127=sum(xk127);
xk128(i)=x(i)^128;
sumxk128=sum(xk128);
xk129(i)=x(i)^129;
sumxk129=sum(xk129);
xk130(i)=x(i)^130;
sumxk130=sum(xk130);
xk131(i)=x(i)^131;
sumxk131=sum(xk131);
xk132(i)=x(i)^132;
sumxk132=sum(xk132);
xk133(i)=x(i)^133;
sumxk133=sum(xk133);
xk134(i)=x(i)^134;
sumxk134=sum(xk134);
xk135(i)=x(i)^135;
sumxk135=sum(xk135);
xk136(i)=x(i)^136;
sumxk136=sum(xk136);
xk137(i)=x(i)^137;
sumxk137=sum(xk137);
xk138(i)=x(i)^138;
sumxk138=sum(xk138);
xk139(i)=x(i)^139;
sumxk139=sum(xk139);
xk140(i)=x(i)^140;
sumxk140=sum(xk140);
xk141(i)=x(i)^141;
sumxk141=sum(xk141);
xk142(i)=x(i)^142;
sumxk142=sum(xk142);
xk143(i)=x(i)^143;
sumxk143=sum(xk143);
xk144(i)=x(i)^144;
sumxk144=sum(xk144);
xk145(i)=x(i)^145;
sumxk145=sum(xk145);
xk146(i)=x(i)^146;
sumxk146=sum(xk146);
xk147(i)=x(i)^147;
sumxk147=sum(xk147);
xk148(i)=x(i)^148;
sumxk148=sum(xk148);
xk149(i)=x(i)^149;
sumxk149=sum(xk149);
xk150(i)=x(i)^150;
sumxk150=sum(xk150);
xk151(i)=x(i)^151;
sumxk151=sum(xk151);
xk152(i)=x(i)^152;
sumxk152=sum(xk152);
xk153(i)=x(i)^153;
sumxk153=sum(xk153);
xk154(i)=x(i)^154;
sumxk154=sum(xk154);
xk155(i)=x(i)^155;
sumxk155=sum(xk155);
xk156(i)=x(i)^156;
sumxk156=sum(xk156);
xk157(i)=x(i)^157;
sumxk157=sum(xk157);
xk158(i)=x(i)^158;
sumxk158=sum(xk158);
xk159(i)=x(i)^159;
sumxk159=sum(xk159);
xk160(i)=x(i)^160;
sumxk160=sum(xk160);
xk161(i)=x(i)^161;
sumxk161=sum(xk161);
xk162(i)=x(i)^162;
sumxk162=sum(xk162);
xk163(i)=x(i)^163;
sumxk163=sum(xk163);
xk164(i)=x(i)^164;
sumxk164=sum(xk164);
xk165(i)=x(i)^165;
sumxk165=sum(xk165);
xk166(i)=x(i)^166;
sumxk166=sum(xk166);
xk167(i)=x(i)^167;
sumxk167=sum(xk167);
xk168(i)=x(i)^168;
sumxk168=sum(xk168);
xk169(i)=x(i)^169;
sumxk169=sum(xk169);
xk170(i)=x(i)^170;
sumxk170=sum(xk170);
xk171(i)=x(i)^171;
sumxk171=sum(xk171);
xk172(i)=x(i)^172;
sumxk172=sum(xk172);
xk173(i)=x(i)^173;
sumxk173=sum(xk173);
xk174(i)=x(i)^174;
sumxk174=sum(xk174);
xk175(i)=x(i)^175;
sumxk175=sum(xk175);
xk176(i)=x(i)^176;
sumxk176=sum(xk176);
xk177(i)=x(i)^177;
sumxk177=sum(xk177);
xk178(i)=x(i)^178;
sumxk178=sum(xk178);
xk179(i)=x(i)^179;
sumxk179=sum(xk179);
xk180(i)=x(i)^180;
sumxk180=sum(xk180);
xk181(i)=x(i)^181;
sumxk181=sum(xk181);
xk182(i)=x(i)^182;
sumxk182=sum(xk182);
xk183(i)=x(i)^183;
sumxk183=sum(xk183);
xk184(i)=x(i)^184;
sumxk184=sum(xk184);
xk185(i)=x(i)^185;
sumxk185=sum(xk185);
xk186(i)=x(i)^186;
sumxk186=sum(xk186);
xk187(i)=x(i)^187;
sumxk187=sum(xk187);
xk188(i)=x(i)^188;
sumxk188=sum(xk188);
xk189(i)=x(i)^189;
sumxk189=sum(xk189);
xk190(i)=x(i)^190;
sumxk190=sum(xk190);
xk191(i)=x(i)^191;
sumxk191=sum(xk191);
xk192(i)=x(i)^192;
sumxk192=sum(xk192);
xk193(i)=x(i)^193;
sumxk193=sum(xk193);
xk194(i)=x(i)^194;
sumxk194=sum(xk194);
xk195(i)=x(i)^195;
sumxk195=sum(xk195);
xk196(i)=x(i)^196;
sumxk196=sum(xk196);
xk197(i)=x(i)^197;
sumxk197=sum(xk197);
xk198(i)=x(i)^198;
sumxk198=sum(xk198);
xk199(i)=x(i)^199;
sumxk199=sum(xk199);
xk200(i)=x(i)^200;
sumxk200=sum(xk200);
xk201(i)=x(i)^201;
sumxk201=sum(xk201);
xk202(i)=x(i)^202;
sumxk202=sum(xk202);
xk203(i)=x(i)^203;
sumxk203=sum(xk203);
xk204(i)=x(i)^204;
sumxk204=sum(xk204);
xk205(i)=x(i)^205;
sumxk205=sum(xk205);
xk206(i)=x(i)^206;
sumxk206=sum(xk206);
xk207(i)=x(i)^207;
sumxk207=sum(xk207);
xk208(i)=x(i)^208;
sumxk208=sum(xk208);
end

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 3 Mar 2020
Edited: Stephen Cobeldick on 3 Mar 2020
Your code is slow and complex because:
  • you expand all of the arrays xk2, xk3, ... xk208 on every loop iteration, which forces MATLAB to shift them in memory on every iteration, which forces your code to be slow.
  • you calculate the sum of the arrays xk2, xk3, ... xk208 on every loop iteration, even though you discard all of those results except for the ones calculated on the final loop iteration. Total waste of time.
  • Using numbered variables is a sign that you are doing something wrong. Using indexing is much more efficient overall, using loops would take less effort.
  • Copy-and-pasting code is a sign that you are doing something wrong. Computers are really only very good at one thing: doing simple operations repeatedly. So when you sit there are copy-and-paste almost-identical code like that, you are just doing the computer's job for it. What a total waste of your time and a total waste of your computer. Instead you should learn how to use arrays (the most powerful and useful feature of MATLAB): https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
For example, I replaced all of your long, slow code with this one line:
>> m = sum(bsxfun(@power,x(:),2:208),1);
And comparing the first value:
>> m(1) % indexing is much better than numbered variables.
ans = 3383.5
>> sumxk2
sumxk2 = 3383.5
and the last value:
>> m(end)
ans = 1.1406e+208
>> sumxk208
sumxk208 = 1.1406e+208
Comparing the other 200 or so variables will demonstrate why using numbered variables is bad code design:
Oh, we might as well look at runtime too:
Elapsed time is 0.057419 seconds. % your code with numbered variables.
Elapsed time is 0.003724 seconds. % my code with BSXFUN and SUM.
  6 Comments
Eddy Iswardi
Eddy Iswardi on 4 Mar 2020
thank you very much. its work.
now, I have a matriks. Can you make a simple code for me? this is my code
clc;
clear all;
x=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.0];
y=[190 187 188 189 190 190 190 190 190 190 192 193 193 193 193 196 196 197 198 200 196 197 197 198 196 192 194 194 193 193 195 196 196 199 197 196 195 196 195 196 196 196 195 194 193 188 190 190 193 190 188 186 185 184 182 185 184 183 183 181 184 185 185 184 185 185 188 189 190 189 187 187 188 188 189 189 188 186 183 176 174 181 181 177 175 175 173 176 178 177 174 174 174 173 173 174 174 178 178 180];
n =length(x);
na=n-1;
my=sum(y);
m = sum(bsxfun(@power,x(:),1:(2*na)),1);
xv = x(1:n);
yv = y(1:n);
ny = sum(bsxfun(@times,bsxfun(@power,xv(:),1:99),yv(:)),1);
for i=1:na
for j=1:na
for k=1:na
A(1,1)=n;
A(i,1)=m(i);
if k>1
A(i,k)=m(k);
if A(i,k)==A(i,k-1)
A(i,k)=m(k+1);
elseif A(i,k)<A(i,k-1)
A(i,k)=m(i+k-1);
end
end
end
end
end
B=[my 1];
for i=2:na
b = ny(i);
B(i)=b;
end
for k=1:na-1
for i=k+1:na
if A(i,k)~=0
lambda=A(i,k)/A(k,k);
for j=1:na
A(i,j)=A(i,j)-lambda*A(k,j);
end
B(i)=B(i)-lambda*B(k);
end
end
end
for i=na:-1:1
sum=0;
X(i)=0;
for j=1:na
sum=sum+A(i,j)*X(j);
end
X(i)=(B(i)-sum)/A(i,i);
end
for c = 1:n
R(c) = 0;
for d = 1:na
R(c) = R(c)+X(d)*x(c)^(d-1);
end
end
R
plot(R)
And how to combine the code whit this
clc;
clear all;
x=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.0];
y=[190 187 188 189 190 190 190 190 190 190 192 193 193 193 193 196 196 197 198 200 196 197 197 198 196 192 194 194 193 193 195 196 196 199 197 196 195 196 195 196 196 196 195 194 193 188 190 190 193 190 188 186 185 184 182 185 184 183 183 181 184 185 185 184 185 185 188 189 190 189 187 187 188 188 189 189 188 186 183 176 174 181 181 177 175 175 173 176 178 177 174 174 174 173 173 174 174 178 178 180];
n =length(x);
na=n-2;
my=sum(y);
m = sum(bsxfun(@power,x(:),1:(2*na)),1);
xv = x(1:n);
yv = y(1:n);
ny = sum(bsxfun(@times,bsxfun(@power,xv(:),1:99),yv(:)),1);
for i=1:na
for j=1:na
for k=1:na
A(1,1)=n;
A(i,1)=m(i);
if k>1
A(i,k)=m(k);
if A(i,k)==A(i,k-1)
A(i,k)=m(k+1);
elseif A(i,k)<A(i,k-1)
A(i,k)=m(i+k-1);
end
end
end
end
end
B=[my 1];
for i=2:na
b = ny(i);
B(i)=b;
end
for k=1:na-1
for i=k+1:na
if A(i,k)~=0
lambda=A(i,k)/A(k,k);
for j=1:na
A(i,j)=A(i,j)-lambda*A(k,j);
end
B(i)=B(i)-lambda*B(k);
end
end
end
for i=na:-1:1
sum=0;
X(i)=0;
for j=1:na
sum=sum+A(i,j)*X(j);
end
X(i)=(B(i)-sum)/A(i,i);
end
for c = 1:n
R(c) = 0;
for d = 1:na
R(c) = R(c)+X(d)*x(c)^(d-1);
end
end
R
plot(R)
the different is only in 'na'. The first code 'na=n-1' and the second code 'na=n-2'. Actually, I want to make 'R' with na value from n (in this case, n=100) until 1. Hope you can help me

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!