Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Image Analyst
Image Analyst il 10 Lug 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.

Più risposte (0)

Categorie

Scopri di più su Entering Commands in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by