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

2 次查看(过去 30 天)
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!

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by