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
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?
回答(1 个)
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.)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!