How to create a multi-index vectors?

18 views (last 30 days)
CaG
CaG on 19 Feb 2019
Edited: Stephen23 on 19 Feb 2019
I have to create some vectors containing the values of a d-degree n-dimensional multi-index. A d-degree n-dimensional multi-index is a n-tuple such that .
Just to give an example, if I want a 2-degree 3-dimensional multi-index, I have to built the vectors:
I can to create them for the 2-dimensional case (basicly, I create a matrix and take the upper-right part), but when I move to higher dimensions I have no clue how to go on.
Do you have some suggestion?

Accepted Answer

Stephen23
Stephen23 on 19 Feb 2019
Edited: Stephen23 on 19 Feb 2019
Start by downloading John D'Errico's excellent partitions function:
and then using it like this:
d = 2;
n = 3;
P = partitions(d,1:d,n);
N = size(P,1);
C = cell(1,N);
for k = 1:N
tmp = repelem(1:d,P(k,:));
tmp(end+1:n) = 0;
C{k} = unique(perms(tmp),'rows');
end
Z = vertcat(C{:})
For d=2 and n=3 this gives:
Z =
0 1 1
1 0 1
1 1 0
0 0 2
0 2 0
2 0 0
For d=3 and n=4 this gives:
Z =
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
0 0 1 2
0 0 2 1
0 1 0 2
0 1 2 0
0 2 0 1
0 2 1 0
1 0 0 2
1 0 2 0
1 2 0 0
2 0 0 1
2 0 1 0
2 1 0 0
0 0 0 3
0 0 3 0
0 3 0 0
3 0 0 0
You might also be interested to read my answer here:

More Answers (0)

Community Treasure Hunt

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

Start Hunting!