How to concatenate these matrices where the below matrices are formed out of permutation?

1 次查看(过去 30 天)
i =
1
1
0
0
i =
1
1
0
0
i =
0
1
1
0
i =
1
1
0
0
i =
0
0
1
1
i =
0
0
1
1

采纳的回答

Rik
Rik 2017-12-30
Why not use this:
i_matrix=perms([0 0 1 1]);
Otherwise, just put it in a matrix inside your loop.
  2 个评论
PLACEIUS NISHIGA G
编辑:PLACEIUS NISHIGA G 2018-1-2
Let k=3,n=5. Permutation formula is, N = (2k−2)! /(k−1)!(k−1)!. I need non-repeating matrices and on concatenating them I need a matrix of size (2k-2 x N).
Rik
Rik 2018-1-2
The output from perms is non-repeating, unlike the things you wrote in your question. What is the output you want? Try to write a bit more. Give an example of what you tried (how did you get the output you put in your question?), and what the result should be.
Have a read here and here.
You might also want to take a look at nchoosek.

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2018-1-2
编辑:Jan 2018-1-2
Do you want the unique set of vector of length n, which contains k 1's and the rest is 0? Then use nchoosek to obtain the indices of the ones:
n = 5;
k = 3;
index = nchoosek(1:n, k);
len = size(index, 1);
Result = zeros(n, len, 'uint8');
Result(sub2ind([n, len], index.', repmat(1:len, k, 1))) = 1
Now the columns of Result are the searched vectors.
Remember that the output is growing rapidly if n gets larger. Calculate the size of len at first by nchoosek(n, k) and stop with an error before you fill the complete RAM and crash the computer.
The output of nchoosek is a a double matrix. If you want a binary matrix, UINT8 or LOGICAL would use 1/8 of the memory only. If this is the bottleneck of your code, there are more efficient methods, which create the output directly. I'm sure the code of https://www.mathworks.com/matlabcentral/fileexchange/24325-combinator-combinations-and-permutations would be useful then.

类别

Help CenterFile Exchange 中查找有关 Elementary Math 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by