Hyper nested for loops
古いコメントを表示
Hi,
I want to create a series of nested "for loops" in order to calculate a constant faster:
my problem is that the function is iterative in such a way that it needs an addictional loop for every step of n:
I would like to build up something like this automatically, i give the code n and it does the rest:
say for example n = 3
it needs to create 3 nested for loops:
for p3 =1:5
for p2 = 1:5
for p1 = 1:5
do something
end
end
end
I'll give you another example, for n = 6 I wnat the code to build up like this:
for p6 = 1:5
for p5 =
.....
you get where this takes
...
end
end
how could I set up something like this? is it possible to do it or do I have to do it manually?
6 件のコメント
Can you say a little more about the problem you're trying to solve using this approach? It may be there is a simpler approach to solve the problem that doesn't require a dynamic number of for loops. For instance, if you were trying to count the number of elements equal to a certain number in an array and you were trying to use one for loop per dimension, there's an easier way to handle that situation.
A = randi(6, [3 4 5]);
n = 0;
for r = 1:size(A, 1)
for c = 1:size(A, 2)
for p = 1:size(A, 3)
if A(r, c, p) == 3
n = n + 1;
end
end
end
end
fprintf("There are %d 3's in A.\n", n)
% or
n2 = sum(A == 3, 'all'); % requires a relatively recent release of MATLAB
fprintf("There are %d 3's in A.\n", n)
% or
n3 = sum(A(:) == 3);
fprintf("There are %d 3's in A.\n", n3)
KALYAN ACHARJYA
2020 年 12 月 7 日
Marco Marchesi comments moved here
I need to build a "n-order" nested loop, just by writing n = some number, but I don't know how and if it possible to do it on matlab.
The idea I came up with should look something like this, but I don't know how to make it work properly:
n = 5; %or some number
up = 10; %or some number
for i = 1:n
%create a deeper level of nesting loops for each iteration
%I need to create n parameters: p1, p2, p3, p4 .... pn and make
% it loop for each parameter for "up" times
%in this case n = 5 so it will be
P (i)= [p_i]
for p_i = 1:up %fifth value of P
for p_i = 1:up %fourth value of P
for p_i = 1:up %third value of P
for p_i = 1:up %second value of P
for p_i=1:up %obviously %first value of Pthe "i" represents a number but
%I don't know if I don't know the syntax or
%am I missing something
%do something
end
end
end
end
end
Better alternatives:
See also:
Steven Lord
2020 年 12 月 7 日
I need to build a "n-order" nested loop, just by writing n = some number,
That's not your end goal. You've focused on a particular approach for how to do what you want to do. Tell us what you want to do and we may be able to suggest a different approach for how to achieve your goal.
In the example I posted, what is "find out how many elements in an array are equal to 3". There are three potential how statements: one with nested for loops, one with a call to sum with the 'all' dimension input, and one with a call to sum on the reshaped array. If I'd fixated on how to implement the solution with nested for loops I might have missed the sum-based approaches.
Or to put it in the context of what you posted, what is "something" in " %do something "? Explain it in words not code.
Marco Marchesi
2020 年 12 月 17 日
Walter Roberson
2020 年 12 月 17 日
Is sum(p_n) a constant? So that the values are "partitions" of a number ? Or are they all individually 0 to the positive number, so you effectively want to check all base-(that number) values with n (base-(that number)) digits ?
回答 (2 件)
Walter Roberson
2020 年 12 月 8 日
0 投票
I give source code for this at https://www.mathworks.com/matlabcentral/answers/623358-get-a-combination-of-unique-paths-for-given-pair-of-numbers#comment_1082638 . The version there is generalized -- you can use an arbitrary number of entries per position, with arbitrary datatype, entries do not need to be consecutive (or numeric).
The code can be simplified a bit for simple numeric cases.
Instead of nesting loop, use one loop and a vector of indices:
n = 6; % n loops
m = 5; % Loops: for p_x = 1:m
p = ones(1, n); % Current index vector, starting at 1
Result = zeros(1, m^n); % Whatever matchs your output...
for k = 1:m^n
Result(k) = sum(p); % dummy for your "do something"
% Increase the index vector:
for ip = 1:n
if p(ip) < m
p(ip) = p(ip) + 1;
break; % Stop "for ip" loop
end
p(ip) = 1; % Reset this index
end
end
Alternatively create all possible combinations of p at once, if it fits into the memory:
n = 6;
m = 5;
c = cell(1, n);
c(:) = {1:m};
[d{1:n}] = ndgrid(c{:});
allp = reshape(cat(n+1, d{:}), [], n);
Now iterate through the rows of allp with one loop.
Note that the size of allp will grow rapidly and you can exhaust the memory easily. m=10 and n = 10 produces an allp of 80 GB. Using UINT8 values would reduce the size to 10 GB, but you see, that this approach is explosive.
カテゴリ
ヘルプ センター および 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!