Effacer les filtres
Effacer les filtres

How to find all possible combination of a digits between first and last digits?

2 vues (au cours des 30 derniers jours)
I have points as 1, 2, 3, 4, 5. The point 1 is the first point, and 5 the last point.
I need to find all possible combination of the possible points between 1 and 5.
I need a result like this:
1 5
1 2 5
1 3 5
1 4 5
1 2 3 5
1 3 2 5
1 2 4 5
1 4 2 5
1 3 4 5
1 4 3 5
1 2 3 4 5
1 3 2 4 5
Ect.
Could anyone help me? Thank you!

Réponse acceptée

Image Analyst
Image Analyst le 10 Juil 2016
Use the perms() function. It sounds a lot like homework. Is it? So here is part of it, the part with all the indexes in there:
m = 1 : 5
indexes = perms([2:4])
m2 = zeros(size(indexes, 1), size(m, 2)) % Initialize output.
for k = 1 : length(indexes)
m2(k,:) = [m(1), m(indexes(k,:)), m(end)];
end
m2 % Print output to command window.
See if you can add the rest to do it for lesser number of indexes.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands 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