Asked by C Zeng
on 23 May 2012

Yesterday I asked how to write code to visit every combination of N-dim vector(with entry 0,1 or 2), and it is very helpful by just trying below:

[C{1:N}]=ndgrid(0:2); M=reshape(vertcat(C{:}),[],N);

Then for i=1:3^N, trying M(i,:) is the answer. However, if I want to add constraint on those 3^N vectors, how to adjust the code?

For example I want all combinations that less than or equal to another vector V(every entry is less than or equal to V's entry). For another one, I want to try those combinations in a order, by least sum of all entries to most.

Thank you for your help!

*No products are associated with this question.*

Answer by Daniel Shub
on 24 May 2012

Accepted answer

I think this question is the same: http://www.mathworks.com/matlabcentral/answers/38130-pick-arrays-from-matrix-with-condition

C Zeng
on 31 May 2012

Daniel, is it possible to look at my question? How to consider a constraint on the combinator and solve it fast?

Related Content

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

Learn moreOpportunities for recent engineering grads.

Apply Today
## 6 Comments

## C Zeng (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/39219#comment_81403

Is there anyone can help me on this problem? Many thanks!

## Daniel Shub (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/39219#comment_81421

Can you give an example of your constraints for a small N (say 3).

## C Zeng (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/39219#comment_81581

Daniel, now I got all combinations of the N-dimension vectors. let me ask you again on this problem:

1. If I got a vector say a, that I want to choose all 3^N rows that <=a, is there a fast way? I know I can add a if command, as a filter when I use fullfact(). But is there a faster way to do so?

2. Also can I get the combinations in the order that the sum is from least to most? I think the output is already in this order right? How can I change the order, say from most to least, or something else?

Many thanks!

## Daniel Shub (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/39219#comment_81583

I don't understand your constraints. Edit your question to include the rule and an example of what rows you want.

## C Zeng (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/39219#comment_81584

Sorry, sure let me give you an example: say N=4; M = fullfact(repmat(3, N, 1))-1;

But I have a vector say a=[1,2,0,1] and I want to list all rows in M that <=a. How can I modify the code? I know that if I add a if-command, I can filter M, but I think it may be slow.

Also, if I want to order all rows in M in a descendant or ascendant order by the sum of vector. How shall I do it? I notice that the output is already in a ascendant order, is that true or convention?

Thanks.

## C Zeng (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/39219#comment_83159

Hello, Daniel, could you answer my question please if there is a better way?

Thank you so much!