How to create a multi-index vectors?
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
CaG
el 19 de Feb. de 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?
0 comentarios
Respuesta aceptada
Stephen23
el 19 de Feb. de 2019
Editada: Stephen23
el 19 de Feb. de 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 comentarios
Más respuestas (1)
Firoozeh
el 26 de Mzo. de 2024
Editada: Stephen23
el 27 de Mzo. de 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 comentario
Stephen23
el 27 de Mzo. de 2024
Editada: Stephen23
el 27 de Mzo. de 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
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!