How to make a series of consecutive numbers using a nested for loop?

Hi, I would like a generic expression to obtain an array of consecutive numbers until reaching the value of the product between the limits of two nested loops. I hope the example below will clarify the question. If I have the nested loop:
for i = 1:3
for j = 1:5
a = ????;
disp(a)
end
end
Which is the expression for 'a' that permits me to obtain a column array from 1 to 15?
a=
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Maybe there are easier way to obtain this but I must use a nested loop and the last number must be equal to the product of the two limits of the for loop (3 and 5 => 15 in this example). Many thanks.

4 Commenti

What you're asking does not make any sense to me. You want an array that goes from 1 to limit_of_loop1 * limit_of_loop2. Therefore, none of the elements in a depend in any way on what happens in the loops. You may as well calculate a outside the loops and if the loops don't do anything get rid of them
Therefore, you could just write:
limit_loop1 = 3;
limit_loop2 = 5;
%for i = 1:limit_loop1
% for j = 1:limit_loop2
%we're just wasting time with these loops!
% end
%end
a = 1:limit_loop1*limit_loop2
Hi Guillame, thanks for the funny solution you proposed. I realize the problem does not make any sense if taken alone. And also that there are many easier ways of achieving what I need. However, I need to solve it in the way I described because I need it in a much wider context that I cannot include in my my description.
I still don't understand what you want. What part of a do you want to calculate in the loop? Would the following work?
limit_loop1 = 3;
limit_loop2 = 5;
for i = 1:limit_loop1
for j = 1:limit_loop2
a = 1:limit_loop1*limit_loop2; %but what's the point?
end
end
Hi, the solution you propose gives as result:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2
1 2 3 4
1 2 3 4 5 6
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9 10
1 2 3
1 2 3 4 5 6
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Which is different from what I am looking for.

Accedi per commentare.

 Risposta accettata

It's not clear to me, what you want to achieve. So let me guess a little bit:
1. The direct approach:
L1 = 3;
L2 = 5;
a = (1:(L1*L2)).'
2. With the loops:
a = [];
c = 0;
for i = 1:3
for j = 1:5
c = c + 1;
a = [a; c]; % Prefer a pre-allocation
disp(a)
end
end
3. Using the paramters mentioned in my first answer:
a = [];
for i = 1:3
for j = 1:5
a = [a; (i - 1) * 5 + j];
disp(a)
end
end
But nothing will beat the approach 1. in speed and clarity.

Più risposte (2)

This looks like a homework question. Do you really want us to solve it, or would delivering the solution made by someone else be cheating?
What about defining a=0 at the beginning and increasing a by one inside the loops?
You can find some constants b, c and d manually, such that
b * i + c * j + d = 1 % for i=1 and b=1
b * i + c * j + d = 2 % for i=1 and b=2
etc

1 Commento

Hi Jan, thanks for your help. This is not at all a homework but a part of a code I am trying to develop in a much wider context. I am sorry but I do not understand how introducing some constants can help. I would like to keep the problem as simple as possible, without introducing new parameters, also because deciding their value could be tricky as well. However, defining a=0 at the beginning is a useful hint. I will further work on this aspect.

Accedi per commentare.

Maybe one of these is what you want. It's really not clear what you want to do within the loops:
Possible solution #1:
limit_loop1 = 3;
limit_loop2 = 5;
a = zeros(limit_loop1 * limit_loop2, 1);
for i = 1:limit_loop1
for j = 1:limit_loop2
aidx = sub2ind([limit_loop1, limit_loop2], i, j);
a(aidx) = aidx;
end
end
Possible solution #2:
limit_loop1 = 3;
limit_loop2 = 5;
a = 1;
for i = 1:limit_loop1
for j = 1:limit_loop2
if i>1 || j>1
a = [a(end); a+1];
end
end
Neither of which make much sense to me, since as pointed out:
a = 1:limit_loop1*limit_loop2
gives the same result without needing the loops

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Tag

Richiesto:

il 10 Feb 2016

Risposto:

Jan
il 14 Feb 2016

Community Treasure Hunt

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

Start Hunting!

Translated by