How to make this loop more efficient?

2 次查看(过去 30 天)
I have a code which has a lot of for loops. I feel that I can make it more efficient by making another loop or by some other method. I'd appreciate it if you help me make this loop more efficient:
clc
clear all
[d1,s,r] = xlsread('alpha-beta-maxslope.csv');
for i=1:3
for j=1:3
for k=1:3
for l=1:3
for m=1:3
for n=1:3
for o=1:3
for p=1:3
beta=[d1(1,i);d1(2,j);d1(3,k);d1(4,l);d1(5,m);d1(6,n);d1(7,o);d1(8,p)];
end
end
end
end
end
end
end
end
  2 个评论
M.B
M.B 2023-4-11
"beta" could be missing some indexing or your code is not complete.
In your code, beta is updated at each iteration.
At the end of your code, beta = [d1(1,3);d1(2,3);d1(3,3);d1(4,3);d1(4,3);d1(4,3);d1(4,3);d1(4,3)]
Could you explain what you plan to do?
Faezeh Manesh
Faezeh Manesh 2023-4-11
This is the complete version of my code. I am trying to find an optimum beta in a regression analysis. I have 3 sets of beta values for each data point and I would like to find for each data point which one of these 3 beta values should I chose in order to get the maximum linear correlation between beta and the other parameter, max-slope.
clc
clear all
[d1,s,r] = xlsread('beta-maxslope.csv');
q=1;
for i=1:3
for j=1:3
for k=1:3
for l=1:3
for m=1:3
for n=1:3
for o=1:3
for p=1:3
for ii=1:3
for jj=1:3
for kk=1:3
for ll=1:3
for mm=1:3
for nn=1:3
for oo=1:3
for pp=1:3
for i1=1:3
for j1=1:3
for k1=1:3
for l1=1:3
for m1=1:3
for n1=1:3
for o1=1:3
for p1=1:3
for i2=1:3
for j2=1:3
for k2=1:3
for l2=1:3
for m2=1:3
for n2=1:3
for o2=1:3
for p2=1:3
for q2=1:3
beta=[d1(1,i);d1(2,j);d1(3,k);d1(4,l);d1(5,m);d1(6,n);d1(7,o);d1(8,p);...
d1(9,ii);d1(10,jj);d1(11,kk);d1(12,ll);d1(13,mm);d1(14,nn);d1(15,oo);d1(16,pp);...
d1(17,i1);d1(18,j1);d1(19,k1);d1(20,l1);d1(21,m1);d1(22,n1);d1(23,o1);d1(24,p1);...
d1(25,i2);d1(26,j2);d1(27,k2);d1(28,l2);d1(29,m2);d1(30,n2);d1(31,o2);d1(32,p2);...
d1(33,q2)];
ml = fitlm(d1(1:33,4),beta);
rsq(q) = ml.Rsquared.Ordinary;
[max_value, max_index] = max(rsq);
if (max_index == q)
ind_ex=[i;j;k;l;m;n;o;p;...
ii;jj;kk;ll;mm;nn;oo;pp;...
i1;j1;k1;l1;m1;n1;o1;p1;...
i2;j2;k2;l2;m2;n2;o2;p2;q2];
max_rsq=max_value;
beta_optimum=[d1(1,i);d1(2,j);d1(3,k);d1(4,l);d1(5,m);d1(6,n);d1(7,o);d1(8,p);...
d1(9,ii);d1(10,jj);d1(11,kk);d1(12,ll);d1(13,mm);d1(14,nn);d1(15,oo);d1(16,pp);...
d1(17,i1);d1(18,j1);d1(19,k1);d1(20,l1);d1(21,m1);d1(22,n1);d1(23,o1);d1(24,p1);...
d1(25,i2);d1(26,j2);d1(27,k2);d1(28,l2);d1(29,m2);d1(30,n2);d1(31,o2);d1(32,p2);...
d1(33,q2)];
end
q=q+1;
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2023-4-11
[d1,s,r] = xlsread('alpha-beta-maxslope.csv');
for i=1:3
suf1 = d1(1,i);
for j=1:3
suf2 = [suf1 d1(2,j)];
for k=1:3
suf3 = [suf2 d1(3,k)];
for l=1:3
suf4 = [suf3 d1(4,l)];
for m=1:3
suf5 = [suf4 d1(5,m)];
for n=1:3
suf6 = [suf5 d1(6,n)];
for o=1:3
suf7 = [suf6 d1(7,o)];
for p=1:3
suf8 = [suf7 d1(8,p)];
beta = suf8;
end
end
end
end
end
end
end
end
This reduces the amount of work to be done per iteration.
Now... if you needed all of the combinations to be in memory simultaneously, then I would have suggested a different solution (and hoped you had enough memory.)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by