How to create a multi-index vectors?

30 views (last 30 days)
CaG
CaG on 19 Feb 2019
Edited: Stephen23 on 27 Mar 2024 at 4:56
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 (1)

Firoozeh
Firoozeh on 26 Mar 2024 at 22:03
Edited: Stephen23 on 27 Mar 2024 at 4:29
function tensor = reconstruct_tensor(matricization_matrix, dimensions, n)
% Input:
% - matricization_matrix: The mode-n matricization matrix
% - dimensions: A vector containing the dimensions of the original tensor
% - n: The mode along which the matricization was performed
% Output:
% - tensor: The reconstructed tensor
% Validate input dimensions
if numel(dimensions) ~= length(dimensions)
error('Dimensions vector should be a 1D array.');
end
% Initialize the tensor
tensor = zeros(dimensions);
% Reshape the matricization matrix into the tensor
tensor = reshape(matricization_matrix, [dimensions(n), prod(dimensions) / dimensions(n)]);
% Permute dimensions to match the original order
perm_order = [n, setdiff(1:length(dimensions), n)];
tensor = permute(tensor, perm_order);
end
tensor = rand(3, 4, 5); % Example tensor
mode = 2; % Mode along which matricization was performed
matrix = matricize(tensor, mode); % Matricization of tensor
original_dimensions = [3, 4, 5]; % Original dimensions of tensor
reconstructed_tensor = reconstruct_tensor(matrix, original_dimensions, mode); % Reconstruct the tensor
Unrecognized function or variable 'reconstruct_tensor'.
what is the problem?
  1 Comment
Stephen23
Stephen23 on 27 Mar 2024 at 4:32
Edited: Stephen23 on 27 Mar 2024 at 4:56
"what is the problem?"
There is no problem running the function here, so most likely you have not saved the function somewhere where MATLAB can see it (e.g. in the current directory, or as a local function at the end of a script).
mode = 2; % Mode along which matricization was performed
matrix = rand(3,4,5);
original_dimensions = [3, 4, 5]; % Original dimensions of tensor
reconstructed_tensor = reconstruct_tensor(matrix, original_dimensions, mode) % Reconstruct the tensor
reconstructed_tensor = 15x4
0.1053 0.4380 0.8521 0.4316 0.0875 0.0092 0.3069 0.1967 0.9221 0.0975 0.9388 0.2141 0.9390 0.7091 0.4043 0.3721 0.8307 0.4997 0.5156 0.1440 0.2715 0.0244 0.2047 0.5679 0.7240 0.3332 0.9244 0.5475 0.7316 0.9049 0.1232 0.1515 0.2695 0.2811 0.2429 0.0977 0.7180 0.9223 0.1026 0.9390
function tensor = reconstruct_tensor(matricization_matrix, dimensions, n)
% Input:
% - matricization_matrix: The mode-n matricization matrix
% - dimensions: A vector containing the dimensions of the original tensor
% - n: The mode along which the matricization was performed
% Output:
% - tensor: The reconstructed tensor
% Validate input dimensions
if numel(dimensions) ~= length(dimensions)
error('Dimensions vector should be a 1D array.');
end
% Initialize the tensor
tensor = zeros(dimensions);
% Reshape the matricization matrix into the tensor
tensor = reshape(matricization_matrix, [dimensions(n), prod(dimensions) / dimensions(n)]);
% Permute dimensions to match the original order
perm_order = [n, setdiff(1:length(dimensions), n)];
tensor = permute(tensor, perm_order);
end

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!