How to create a multi-index vectors?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponse acceptée
Stephen23
le 19 Fév 2019
Modifié(e) : Stephen23
le 19 Fév 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:
0 commentaires
Plus de réponses (1)
Firoozeh
le 26 Mar 2024
Modifié(e) : Stephen23
le 27 Mar 2024
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 commentaire
Stephen23
le 27 Mar 2024
Modifié(e) : Stephen23
le 27 Mar 2024
"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
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
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!