Given a matrix such as
[1 2
3 4]
I am looking to create every possible sequence in that matrix and store them in a single matrix.
So in this case it would be
[1 2]
[1 4]
[3 2]
[3 4]
I managed to do the above using two nested for-loops and it worked fine.
But the problem is that I am dealing with a 3x11 matrix and I am looking to find every possible unique sequences.
I tried the nested for-loop. It works but takes way too much time. I need to perform this action more than 50 times in my real problem so cpu time is very important.
Is there anyway to change the code to make it go faster?
Here is the nested for loop I wrote.
clear;clc
s = rand(3,11,"double");
all_seq = zeros(1,11);
iteration = 1;
for a = 1:3
for b = 1:3
for c = 1:3
for d = 1:3
for e = 1:3
for f = 1:3
for g = 1:3
for h = 1:3
for i = 1:3
for j = 1:3
for k = 1:3
c_seq(1) = s(a,1);
c_seq(2) = s(b,2);
c_seq(3) = s(c,3);
c_seq(4) = s(d,4);
c_seq(5) = s(e,5);
c_seq(6) = s(f,6);
c_seq(7) = s(g,7);
c_seq(8) = s(h,8);
c_seq(9) = s(i,9);
c_seq(10) = s(j,10);
c_seq(11) = s(k,11);
all_seq(iteration,:) =c_seq;
iteration =iteration+1;
end
end
end
end
end
end
end
end
end
end
end

回答 (1 件)

KSSV
KSSV 2020 年 6 月 11 日

0 投票

Read about nchoosek.

カテゴリ

質問済み:

2020 年 6 月 11 日

編集済み:

2020 年 6 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by