MATLAB Answers

nested k for loops for a parameter k

1 view (last 30 days)
Minoo Zarsav
Minoo Zarsav on 23 Aug 2020
Edited: Jan on 23 Aug 2020
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.

Accepted Answer

Jan
Jan on 23 Aug 2020
Edited: Jan on 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 Comments

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 23 Aug 2020

  1 Comment

Minoo Zarsav
Minoo Zarsav on 23 Aug 2020
Thanks alot for your answer but this is not what I meant. I need to write pice of code like this
for v1=1:2
for v2=1:2
for u3=1:2
for u4=1:2
for w1=1:2
for w2=1:2
for u5=1:2
for u6=1:2
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;
end
end
end
end
end
end
end
end
This is when we know k= 6. but now I want to write it for the general case. It is not a nested function. I just need to write k for loops to define a multi-dimensional array.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!