# Trying to build an array of row vectors such that every possible combination of the 3 vector components is included

1 vue (au cours des 30 derniers jours)
Michael Costa le 7 Mai 2021
Commenté : Michael Costa le 7 Mai 2021
Take a vector [i j k]. I want to build an array of such vectors, with each element ranging from 1:20, such that every possible combination of the elements is included in the array. For example, the array would start with [1 1 1], [2 1 1], [3 1 1], and continue until [20 20 20] is reached. Here is what I have started with.
clear;
Ax_A = [];
for i = 1:20
Ax_A(i,1) = i;
end
for j = 1:20
Ax_A(:,2) = j
end
for k = 1:20
Ax_A(:,3) = k
end
When I don't supress the output for the 2nd (jth) and 3rd (kth) elements, it seems to be iterating through every single combination that I want. However, it is not storing all of these in the whole array that I am trying to build. If I do suppress the outputs, the result is just a 20x3 with i = 1-20, j = 20, and k = 20.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

David Fletcher le 7 Mai 2021
Modifié(e) : David Fletcher le 7 Mai 2021
If you are going the loop route, then you want all three loops to be nested, rather than three separate loops. Something more like this:
clear;
Ax_A = [];
for i = 1:20
page=[];
for j = 1:20
for k = 1:20
page=[page; k j i];
end
end
Ax_A(:,:,i)=page;
end
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Michael Costa le 7 Mai 2021
Thanks David! works perfectly.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Creating and Concatenating Matrices 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