(continued)On the combination of N-dim vector.

1 回表示 (過去 30 日間)
C Zeng
C Zeng 2012 年 5 月 23 日
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!
  6 件のコメント
C Zeng
C Zeng 2012 年 5 月 25 日
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
C Zeng 2012 年 6 月 5 日
Hello, Daniel, could you answer my question please if there is a better way?
Thank you so much!

サインインしてコメントする。

採用された回答

Daniel Shub
Daniel Shub 2012 年 5 月 24 日
  1 件のコメント
C Zeng
C Zeng 2012 年 5 月 31 日
Daniel, is it possible to look at my question? How to consider a constraint on the combinator and solve it fast?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by