How to make this loop more efficient?
2 views (last 30 days)
Show older comments
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 Comments
M.B
on 11 Apr 2023
"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?
Answers (1)
Walter Roberson
on 11 Apr 2023
[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 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!