Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

Image Analyst
Image Analyst am 10 Jul. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by