(Ask again)Show every combination of N-dim vector.

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 Kommentare

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.
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.
@Sean you can double check your solution with unique(M, 'rows'), and it appears answers are repeated.
huh, your fullfact one is better anyway.
C Zeng
C Zeng am 25 Mai 2012
Yes, Sean, your code gives 3^N rows, but they have repeated one. Also I do not understand what does it mean? It seems that it is going to construct a grid in graphics, right? But I want all factorial combinations.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Daniel Shub
Daniel Shub am 24 Mai 2012

3 Stimmen

I will repeat the answer I gave to your original question
x = fullfact([3,3,3])-1
EDIT
For a general N
N = 3;
x = fullfact(repmat(3, N, 1))-1

9 Kommentare

One remark: needs Stat Toolbox.
I will add the wiki: http://en.wikipedia.org/wiki/Design_of_experiments
Alternatively a very useful FEX submission: http://www.mathworks.com/matlabcentral/fileexchange/24325-combinator-combinations-and-permutations
@Daniel: thank you, always glad to learn somethign new!
@Oleg it is worth taking a look at the code of fullfact. Usually I can have a vague idea what a function is doing pretty quickly, in this case I have no idea.
"Doesn't matter had s.."
Somehow the citation sounds legit.
C Zeng
C Zeng am 25 Mai 2012
Daniel, thank you! If the dimension is N, how can I change it to N-dim vector? Say fullfact([3,3,3...,3])-1?
Yes, or even better repmat(3, N, 1)
C Zeng
C Zeng am 25 Mai 2012
Thank you, Oleg, your links are awesome!
C Zeng
C Zeng am 25 Mai 2012
Daniel, I am confused here, your full factorial design works for my purpose. I am asking whether I can set the dimension N, so I do not need to input 3 N times. repmat(3, N, 1) does not give what I want here. Can you advise?
See my edit.
C Zeng
C Zeng am 25 Mai 2012
Oh, yes, or using combinator.m file:
combinator(3,N)-1
Thank you so much!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by