How to create a multi-index vectors?
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Stephen23
2019 年 2 月 19 日
編集済み: Stephen23
2019 年 2 月 19 日
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 件のコメント
その他の回答 (1 件)
Firoozeh
2024 年 3 月 26 日
編集済み: Stephen23
2024 年 3 月 27 日
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 件のコメント
Stephen23
2024 年 3 月 27 日
編集済み: Stephen23
2024 年 3 月 27 日
"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
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!