how to generate a matrix with all possible combinations in an efficient way

조회 수: 1 (최근 30일)
mohamed Faraj
mohamed Faraj 2019년 8월 30일
답변: Guillaume 2019년 8월 30일
I have n=1:4 and m=3, and I want to generate a matrix S whose rows are all the possible combinations.. i.e., S has 4^3 rows and 3columns. What is the mist efficient way to do this as I want to extend matrix to be of 50^10 rows and 10 columns (very large)
  댓글 수: 3
Guillaume
Guillaume 2019년 8월 30일
very large is a massive understatement, as Bruno says. Looking a this wikipedia entry, you might be able to store your matrix if you manage to convice google to lend you all of their data centres. Good luck!
mohamed Faraj
mohamed Faraj 2019년 8월 30일
I agree with you. The matrix may be of a manegable size. e.g., 20^5 or 10^5 rows and 5 columns

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2019년 8월 30일
For reasonable n and m you can either use Jos' allcomb or the following:
%n: a vector of numbers
%m: a scalar integer
allcombs = cell(1, m);
[allcombs{:}] = ndgrid(n);
allcombs = reshape(cat(m+1, allcombs{:}), [], m);

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by