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)
There are 5 3's in A.
% or
n2 = sum(A == 3, 'all'); % requires a relatively recent release of MATLAB
fprintf("There are %d 3's in A.\n", n)
There are 5 3's in A.
% or
n3 = sum(A(:) == 3);
fprintf("There are %d 3's in A.\n", n3)
There are 5 3's in A.
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
Steven Lord
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
Marco Marchesi 2020년 12월 17일
ok, you're right I'll try my best to explain it clearly:
I need to verify for some values whether a number defined by a function is an integer or not:
the function is actually a succession, where the n-th number is defined as following:
a_n = [3^(n-1) +2 * 3^(n-1) *2^p_1 + ... + 2^(n-1) *2^p_(n-1)] / (2^n * 2^p_n -3^n)
The problem is that p_1, p_2 ... p_n are not defined parameters and cannot be determined first. The only thing I know is that they can vary from 0 to some positive integer (all ps' are integers, think of them as some kind of experimental values). That's why I thought maybe varying them through a dynamic set of nested for loops could be a great idea, because you will notice that every next iteration, the succession generates a new p_i coefficient that can vary through some limit parameters. I'm open to any new suggestions.
The ultimate goal would be finding if this succession can contain integers or not on an infinitely large scale, so if someone knows some method to solve this problem algebraically, it would be great.
thanks for your support!
Walter Roberson
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
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.
Jan
Jan 2020년 12월 17일
편집: Jan 2020년 12월 18일

0 개 추천

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에 대해 자세히 알아보기

질문:

2020년 12월 6일

편집:

Jan
2020년 12월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by