How to create an N-ary array
조회 수: 4 (최근 30일)
이전 댓글 표시
Suppose I have K variables, each of which can take the values 1 through N. I want to create a table such that each column corresponds to the Kth variable, and each row corresponds to a particular combination. This table will be N^K entries long and K entries wide. One way to do this would be, for example
[V1 V2 ... VK] = ngrid(1:N,1:N,...,1:N)
V = [V1 V2 ... VK]
However, this clearly does not generalize nicely to variable N. Is there a simple way to extend this code so it works for different K?
댓글 수: 8
James Tursa
2020년 12월 18일
편집: James Tursa
2020년 12월 18일
OK, got it. Looks like you are basically counting in base N with K digits. How large can K and N be? This could easily eat all your memory if they are too large.
답변 (2개)
Bruno Luong
2020년 12월 19일
N=3
K=5
[~,A] = ismember(dec2base(0:N^K-1,N),['0':'9' 'A':'Z']);
A = A-1
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!