Generating Array from nested loops

12 vues (au cours des 30 derniers jours)
jesus escareno
jesus escareno le 21 Avr 2017
Commenté : jesus escareno le 23 Avr 2017
So Ive been trying to create an array of 4 numbers using nested loops The first number goes from 1:5 The second number goes from 1:2 The third number goes from 1:3 The fourth number goes from 1:4
so that
A= [1 1 1 1
1 2 1 1
1 2 2 1
1 2 2 2
1 3 1 1
1 3 2 1
. . . .
. . . .
5 2 3 4 ]
So i been trying to use nested loops to generate that example
for n1 = 1: 5
n(n1,:) = [n1]
for n2 = 1: 2
n(n2,:) = [n2]
end
end
But this replaces my previous input. How do i make so i make that 4 element array of all possible values.

Réponse acceptée

Stephen23
Stephen23 le 21 Avr 2017
Modifié(e) : Stephen23 le 21 Avr 2017
Download combinations or allcomb from MATLAB FEX:
>> sortrows(combinations(1:5,1:2,1:3,1:4))
ans =
1 1 1 1
1 1 1 2
1 1 1 3
1 1 1 4
1 1 2 1
1 1 2 2
1 1 2 3
1 1 2 4
1 1 3 1
1 1 3 2
1 1 3 3
1 1 3 4
1 2 1 1
1 2 1 2
1 2 1 3
1 2 1 4
1 2 2 1
1 2 2 2
1 2 2 3
1 2 2 4
1 2 3 1
1 2 3 2
1 2 3 3
1 2 3 4
2 1 1 1
2 1 1 2
2 1 1 3
2 1 1 4
2 1 2 1
2 1 2 2
2 1 2 3
2 1 2 4
2 1 3 1
2 1 3 2
2 1 3 3
2 1 3 4
2 2 1 1
2 2 1 2
2 2 1 3
2 2 1 4
2 2 2 1
2 2 2 2
2 2 2 3
2 2 2 4
2 2 3 1
2 2 3 2
2 2 3 3
2 2 3 4
3 1 1 1
3 1 1 2
3 1 1 3
3 1 1 4
3 1 2 1
3 1 2 2
3 1 2 3
3 1 2 4
3 1 3 1
3 1 3 2
3 1 3 3
3 1 3 4
3 2 1 1
3 2 1 2
3 2 1 3
3 2 1 4
3 2 2 1
3 2 2 2
3 2 2 3
3 2 2 4
3 2 3 1
3 2 3 2
3 2 3 3
3 2 3 4
4 1 1 1
4 1 1 2
4 1 1 3
4 1 1 4
4 1 2 1
4 1 2 2
4 1 2 3
4 1 2 4
4 1 3 1
4 1 3 2
4 1 3 3
4 1 3 4
4 2 1 1
4 2 1 2
4 2 1 3
4 2 1 4
4 2 2 1
4 2 2 2
4 2 2 3
4 2 2 4
4 2 3 1
4 2 3 2
4 2 3 3
4 2 3 4
5 1 1 1
5 1 1 2
5 1 1 3
5 1 1 4
5 1 2 1
5 1 2 2
5 1 2 3
5 1 2 4
5 1 3 1
5 1 3 2
5 1 3 3
5 1 3 4
5 2 1 1
5 2 1 2
5 2 1 3
5 2 1 4
5 2 2 1
5 2 2 2
5 2 2 3
5 2 2 4
5 2 3 1
5 2 3 2
5 2 3 3
5 2 3 4

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by