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!