nested k for loops for a parameter k
조회 수: 1 (최근 30일)
이전 댓글 표시
How can I make k nested for loops when k is a parameter?!
I try to write a function with some parameter and I need to write k for loops when I don't know k and give it just as input.
댓글 수: 1
Stephen23
2020년 8월 23일
etc., easy to find by searching this forum (the search field is in the top right of the page).
채택된 답변
Jan
2020년 8월 23일
편집: Jan
2020년 8월 23일
Start with simplyfying your code: Replace the 2 innermost loops:
s = 0;
for u1=1:2
for u2=1:2
s = s + t1(u1,v1,w1)*t2(u2,v2,w2)*x1(u1,u2,u3,u4,u5,u6);
end
end
y1(v1,v2,u3,u4,w1,w2,u5,u6) = s;
by
y1(v1,v2,u3,u4,w1,w2,u5,u6) = ...
sum(t1(:, v1, w1) .* t2(:, v2, w2).' .* x1(:, :, u3,u4,u5,u6), 'all');
This is remarkably slower. What a pity.
A further simplification for the whole code:
y = zeros(2,2,2,2,2,2,2,2); % Preallocation!!!
for v1 = 1:2
for v2 = 1:2
for w1 = 1:2
for w2 = 1:2
y1(v1, v2, :, :, w1, w2, :, :) = ...
sum(t1(:, v1, w1) * t2(:, v2, w2).' .* x1, [1, 2]);
end
end
end
end
Now you have 4 loops only. It is possible to remove them also using some reshape commands, but tedious. Alternatively you can use this approach:
fcn = @(v) sum(t1(:, v(1), v(2)) * t2(:, v(3), v(4)).' .* x1, [1, 2]);
C = NestedLoops([1,1,1,1], [2,2,2,2], fcn);
M = reshape(cell2mat(C), [2,2,2,2,2,2,2,2]);
M = permute(M, [1,4,3,5,2,6,7,8]);
function Result = NestedLoops(ini, fin, fcn)
nv = fin - ini + 1;
nvProd = prod(nv);
Result = cell([nv, 1]); % Avoid creation of {nv x nv} cell matrix
iR = 0;
n = numel(ini);
v = ini(:).'; % Current index vector as row
for k = 1:nvProd
% Call the function with the index vector as first input:
iR = iR + 1;
Result{iR} = fcn(v);
% Increase index vector:
for iv = 1:n
if v(iv) < fin(iv)
v(iv) = v(iv) + 1;
break; % Stop "for k" loop
end
v(iv) = ini(iv); % Reset this index
end
end
Well, I admit that this is not nice. Most of all it is not trivial to expand it to more dimensions and the permutation needed to join the output cell is ugly.
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!