How to generate an *M^N* by *N* integer matrix consiting of all possible length N row vectors whose elements are between 0 and M-1?

1 回表示 (過去 30 日間)
I need to generate an M^N by N matrix consisting of all possible length N (N>=2) row vectors whose elements are integer numbers between 0 and M-1 (M>=2). Since there are a total of M^N such row vectors, the matrix has M^N rows, each corresponding to one particular row vector. I also want the row vectors in an "incremental" order, for example, when M = 2 and N = 3, I want to generate the following matrix:
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
In my code, a typical value of M would be 16 and N would be 3. So my matrix would be a large matrix. Does anybody know how to generate such a matrix in an efficient way?
Thanks

採用された回答

Jan
Jan 2013 年 4 月 8 日
There are several corresponding submissions in the FileExchange. When speed matters, use FEX: VChooseKRO. When you do not want to compile C-code, try FEX: combinator.
Using UINT8 arrays will save a lot of RAM and processing time.
  2 件のコメント
Peng Liu
Peng Liu 2013 年 4 月 8 日
Thank you so much! This is exactly what I want
Peng Liu
Peng Liu 2013 年 4 月 13 日
編集済み: Peng Liu 2013 年 4 月 13 日
Hi Jan,
With your help I solved this problem. Recently, I also solve this problem in a different way using Matlab built-function. So I would like to share it with you.
M = 2; N = 3; % M^N by N integer matrix
levels = M*ones(1,N);
Int_Matrix = fliplr(fullfact(levels))-1

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 4 月 5 日
https://groups.google.com/group/comp.lang.c/browse_thread/thread/d98842738fa27a55/2f6d5ff877063de1
with the adjustment by Chris Torek.
I don't think I have posted the MATLAB code, but it is not difficult to write.
  1 件のコメント
Peng Liu
Peng Liu 2013 年 4 月 8 日
Thanks for you reply. But I couldn't find the answer to my question from the link. Could you provide the Matlab code?

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by