Can anyone write a function that generates a permutation matrix from zero to a x number in this way? For example:
myfunc(x,y) where x is the max number and y is the number of columns. For example:
myfunc(2,3) and generates this result:
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
2 0 0
2 0 1
2 1 0
2 0 1
1 2 0
...
all the way to 222
It's for testing p d q values of a ARIMA(p,d,q) model
CodePudding user response:
If you know that x is 9 or less, then you could use a base conversion function as follows:
function z = myfunc(x,y)
z = dec2base(0:((x 1)^y-1),x 1) - '0';
end
If x could be larger than 9, then more generic code would be required:
function z = myfunc(x,y)
z = arrayfun(@(c)base2dec(c,x 1),dec2base(0:((x 1)^y-1),x 1));
end
In both cases, note that if the x and y are too large then the conversions will not work and/or you will run out of memory. In either case, the basic strategy is to simply count in base (x 1) from 0 to the total that you want, which is (x 1)^y-1.
CodePudding user response:
You can try this code:
function array_rand = myfunc(max_num, col_num, row_num)
array_rand = cell(row_num,col_num)
for i=1:row_num
for j=1:col_num
array_rand(i,j) = num2cell(randperm(max_num, 1)-1)
end
end
end
