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
0 个评论
采纳的回答
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 个评论
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.
更多回答(1 个)
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!