nested k for loops for a parameter k
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Stephen23
am 23 Aug. 2020
etc., easy to find by searching this forum (the search field is in the top right of the page).
Akzeptierte Antwort
Jan
am 23 Aug. 2020
Bearbeitet: Jan
am 23 Aug. 2020
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 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!