Asked by C Zeng
on 24 May 2012

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!

*No products are associated with this question.*

Answer by Daniel Shub
on 24 May 2012

Accepted Answer

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

C Zeng
on 25 May 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?

Daniel Shub
on 25 May 2012

See my edit.

C Zeng
on 25 May 2012

Oh, yes, or using combinator.m file:

combinator(3,N)-1

Thank you so much!

Log in to comment.

Answer by Daniel Shub
on 24 May 2012

I think http://www.mathworks.com/matlabcentral/fileexchange/10064-allcomb might do it also.

Log in to comment.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 5 Comments

## Oleg Komarov (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/39358-ask-again-show-every-combination-of-n-dim-vector#comment_81415

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 (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/39358-ask-again-show-every-combination-of-n-dim-vector#comment_81418

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 (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/39358-ask-again-show-every-combination-of-n-dim-vector#comment_81420

@Sean you can double check your solution with unique(M, 'rows'), and it appears answers are repeated.

## Sean de Wolski (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/39358-ask-again-show-every-combination-of-n-dim-vector#comment_81425

huh, your fullfact one is better anyway.

## C Zeng (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/39358-ask-again-show-every-combination-of-n-dim-vector#comment_81561

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.

Log in to comment.