(Ask again)Show every combination of N-dim vector.
Show older comments
Hello, I asked it the day before yesterday but the code some expert told does not work well.
He wrote:
[C{1:N}]=ndgrid(0:2);
M=reshape(vertcat(C{:}),[],N);
However, it does show 3^N rows, but not every combination of N-dim vector of {0,1,2}. Is there another way to show all 3^N combinations and put it in a matrix?
Thank you so much!
5 Comments
Oleg Komarov
on 24 May 2012
Please familiarize with the formatting capabilites on: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Especially with the code section.
Sean de Wolski
on 24 May 2012
How does this not work?
If I use N = 4; I get an 81x4 matrix of all possibilities. 3^N is 81.
etc. for all N.
Please elaborate on what doesn't work and please post this as a comment in your other question.
Daniel Shub
on 24 May 2012
@Sean you can double check your solution with unique(M, 'rows'), and it appears answers are repeated.
Sean de Wolski
on 24 May 2012
huh, your fullfact one is better anyway.
C Zeng
on 25 May 2012
Accepted Answer
More Answers (1)
Daniel Shub
on 24 May 2012
1 vote
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!