How to create an N-ary array

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
James Tursa 2020 年 12 月 18 日
How do you have your K variables stored? Hopefully not as named-numbered variables. In a cell array or as columns of a matrix?
David Cyncynates
David Cyncynates 2020 年 12 月 18 日
編集済み: David Cyncynates 2020 年 12 月 18 日
I called them variables just because it makes sense to think of them that way to me, but ultimately I just want to have a N^K by K array with each combination stored in a given row. I suppose I could do this the old-fashioned way, where I create the matrix row by row, but I feel like there aught to be a better way.
James Tursa
James Tursa 2020 年 12 月 18 日
That doesn't answer my question. How do you have them currently stored?
David Cyncynates
David Cyncynates 2020 年 12 月 18 日
編集済み: David Cyncynates 2020 年 12 月 18 日
They're not stored. I would like it to be stored in an array. To be clear - this is my point of confusion! It feels very cumbersome to make this object in matlab, so I think I am missing something.
James Tursa
James Tursa 2020 年 12 月 18 日
OK, I will ask it another way. Are the inputs always the exact values 1:N?
David Cyncynates
David Cyncynates 2020 年 12 月 18 日
Yes, the only informatino you need to create this array is the values N and K: Basically, each row stores a number written in N symbols.
David Cyncynates
David Cyncynates 2020 年 12 月 18 日
Here's an example of what I was trying to write. Perhaps I'll just stick with this:
N = 4;
K = 3;
V = zeros(K,N^K);
ii = 0 : N^K - 1;
for jj = 1 : K
V(jj,:) = floor((mod(ii,N^jj))/N^(jj-1));
end
James Tursa
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 件)

Stephen23
Stephen23 2020 年 12 月 18 日
編集済み: Stephen23 2025 年 7 月 27 日

0 投票

N = 4;
K = 3;
C = cell(1,K);
[C{:}] = ndgrid(1:N);
M = reshape(cat(K+1,C{:}),[],K)
M = 64×3
1 1 1 2 1 1 3 1 1 4 1 1 1 2 1 2 2 1 3 2 1 4 2 1 1 3 1 2 3 1 3 3 1 4 3 1 1 4 1 2 4 1 3 4 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
How it works:
As James Tursa pointed out, you could quickly run out of memory for large values of K.

4 件のコメント

Paul
Paul 2025 年 7 月 27 日
Hi Stephen,
I thought this would work, but it throws an error:
N = 4;
K = 3;
try
T = combinations(struct('a',repmat({1:N},1,K)).a)
catch ME
ME.message
end
ans = 'Argument number is not valid.'
But this works:
C = repmat({1:N},1,K);
T = combinations(C{:});
Is it not the case that both calls to combinations use the same comma-separated-list on input?
It appears the issue is with a call to inputname in combinations
try
inputnametest(struct('a',repmat({1:N},1,K)).a)
catch ME
ME.message
end
nargin =
ans = 3
varargin = 1×3 cell array
{[1 2 3 4]} {[1 2 3 4]} {[1 2 3 4]}
ans = 'Argument number is not valid.'
Why is 2 not a valid input argument number when nargin == 3?
But this works fine:
inputnametest(C{:})
nargin =
ans = 3
varargin = 1×3 cell array
{[1 2 3 4]} {[1 2 3 4]} {[1 2 3 4]}
ans = 0×0 empty char array
function inputnametest(varargin)
disp('nargin = ');nargin
varargin
inputname(2)
end
Stephen23
Stephen23 2025 年 7 月 27 日
編集済み: Stephen23 2025 年 7 月 28 日
@Paul: that appears to be a bug in how the fun(struct(..).fieldname) syntax is parsed when the function FUN just happens to call INPUTNAME. It seems to not depend on COMBINATIONS in particular as it also occurs with other functions that call INPUTNAME, e.g. TABLE:
S = struct('a',{1,2});
T = table(S.a) % this works (two lines)...
T = 1×2 table
Var1 Var2 ____ ____ 1 2
[X,Y] = ndgrid(struct('a',{1,2}).a) % and this too (no INPUTNAME)...
X = 1
Y = 2
T = table(struct('a',{1,2}).a) % yet this does not.
Error using inputname
Argument number is not valid.

Error in table (line 159)
for i = 1:numVars, varnames{i} = inputname(i); end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I reported the bug. My guess is that it has something to do with how the number of input names are identified from a static code analysis, and perhaps has never been updated for this (still relatively new) syntax.
Paul
Paul 2025 年 7 月 27 日
Would you mind posting back here the outcome of the bug report? Thx.
Stephen23
Stephen23 2025 年 8 月 1 日
@Paul: TMW confirmed that it is a bug, which apparently arose due to changes made in R2020b. It will get fixed in a future release.

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

Bruno Luong
Bruno Luong 2020 年 12 月 19 日

0 投票

N=3
K=5
[~,A] = ismember(dec2base(0:N^K-1,N),['0':'9' 'A':'Z']);
A = A-1

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

タグ

質問済み:

2020 年 12 月 18 日

コメント済み:

2025 年 8 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by