How to create an N-ary array
Afficher commentaires plus anciens
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 commentaires
James Tursa
le 18 Déc 2020
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
le 18 Déc 2020
Modifié(e) : David Cyncynates
le 18 Déc 2020
James Tursa
le 18 Déc 2020
That doesn't answer my question. How do you have them currently stored?
David Cyncynates
le 18 Déc 2020
Modifié(e) : David Cyncynates
le 18 Déc 2020
James Tursa
le 18 Déc 2020
OK, I will ask it another way. Are the inputs always the exact values 1:N?
David Cyncynates
le 18 Déc 2020
David Cyncynates
le 18 Déc 2020
James Tursa
le 18 Déc 2020
Modifié(e) : James Tursa
le 18 Déc 2020
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.
Réponses (2)
N = 4;
K = 3;
C = cell(1,K);
[C{:}] = ndgrid(1:N);
M = reshape(cat(K+1,C{:}),[],K)
How it works:
As James Tursa pointed out, you could quickly run out of memory for large values of K.
4 commentaires
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
But this works:
C = repmat({1:N},1,K);
T = combinations(C{:});
try
inputnametest(struct('a',repmat({1:N},1,K)).a)
catch ME
ME.message
end
Why is 2 not a valid input argument number when nargin == 3?
But this works fine:
inputnametest(C{:})
function inputnametest(varargin)
disp('nargin = ');nargin
varargin
inputname(2)
end
@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)...
[X,Y] = ndgrid(struct('a',{1,2}).a) % and this too (no INPUTNAME)...
T = table(struct('a',{1,2}).a) % yet this does not.
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
le 27 Juil 2025
Would you mind posting back here the outcome of the bug report? Thx.
Bruno Luong
le 19 Déc 2020
N=3
K=5
[~,A] = ismember(dec2base(0:N^K-1,N),['0':'9' 'A':'Z']);
A = A-1
Catégories
En savoir plus sur Dates and Time dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!