Convert unfolded tensor back to higher dimensional tensor?

조회 수: 8 (최근 30일)
Rohit Gupta
Rohit Gupta 2018년 6월 5일
댓글: Kenneth Benicio 2022년 3월 20일
I use below function to convert tensor into unfolded matrix
function X = unfold(A,n)
d = size(A)
X = reshape(shiftdim(a,n-1), d(n), []);
end
But I am unable to understand how to convert a matrix back to tensor Sample code
function X = fold(A,n,d)
% d is array containing dimension
% n is order of folding
% A is unfolded matrix
end
  댓글 수: 2
mona ismail
mona ismail 2020년 5월 8일
hello,
I tried the first code (unfolding one) and it is working only for 1- and 3-mode but not for 2-mode matricization.
do you have any idea how can I get the 2-mode also?
Kenneth Benicio
Kenneth Benicio 2022년 3월 20일
A bit late but could help someone in the future. I had the same problem with the 2-mode unfold and after some research I figure out that it's possible to write the unfolding of a tensor with this function
dim = size(tensor);
order = 1:numel(dim);
order(mode) = [];
order = [mode order];
mtx = reshape(permute(tensor,order), dim(mode), prod(dim)/dim(mode));

댓글을 달려면 로그인하십시오.

답변 (1개)

Aakash Deep
Aakash Deep 2018년 6월 5일
Hey Rohit,
Let's first see how we can unfold a tensor to matrix then you might get some insight how to unfold it back. Let's suppose you have a tensor A with dimensions x*y*z (assume, x=4, y=3, z=2).
A(:,:,1) = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
and
A(:,:,2) = [21 22 23; 24 25 26; 27 28 29; 30 31 32]
Now, you can unfold it in 3 ways that means you can unfold it any of the three dimension. So when you unfold it you will get one of the below possible combination.
A1 (x * yz) = [1 2 3 21 22 23; 4 5 6 24 25 26; 7 8 9 27 28 29; 10 11 12 30 31 32]
In this, all the slices gets placed side by side horizontally.
A2 (y * xz) = [1 4 7 10 21 24 27 30; 2 5 8 11 22 25 28 31; 3 6 9 12 23 26 29 32]
In this, transpose of all the slices concatenates side by side horizontally.
A3(z * xy) = [1 2 3 4 5 6 7 8 9 10 11 12; 21 22 23 24 25 26 27 28 29 30 31 32]
In this, all the slices gets converting into a vector and then these vectors concatenates vertically.
Now, you have to figure it out in which way you can unfold your matrix such that it gets converted into a tensor.
Hope this helps :)
  댓글 수: 5
Aakash Deep
Aakash Deep 2018년 6월 6일
From the given dimension I can clearly see that this is similar to the case as of A2 in the above given example. For this case, the below code will work. Assume A2 is your matrix and B is your tensor.
x = 12;
y = 50;
z = 40;
slice = size(A2,2)/x;
part = size(A2,2)/z;
start=1;
for i=1:slice
B(:,:,i)=A2(:,start:start+part-1)';
start=start+part;
end
Hope this helps :)
snowflake
snowflake 2018년 12월 4일
Hello,
I'm trying to implement the case 1 for this matrix:
B =
1 10 100 2 20 200
3 30 300 4 40 400
and it works, it shows me this tensor:
A(:,:,1) =
1 2
3 4
A(:,:,2) =
10 20
30 40
A(:,:,3) =
100 200
300 400
the code I adapted from you:
x=2; y=2; z=3;
slice = size(B,2)/y;
part = size(B,2)/z;
start=1;
A=zeros(x,y,z);
for i=1:slice
A(:,:,i)=B(:,[start start+part+1]);
start=start+1;
end
The problem is it only works for a 2x6 matrix. If I transform B into a 2x9, I'd have to modify one line of code like this so that I tell the program which columns I want:
A(:,:,i)=B(:,[start start+part start+part+3]);
and it would produce the desired outcome:
A(:,:,1) =
1 2 3
4 5 6
A(:,:,2) =
10 20 30
40 50 60
A(:,:,3) =
100 200 300
400 500 600
for
B =
1 10 100 2 20 200 3 30 300
4 40 400 5 50 500 6 60 600
Any idea how I could code it so that it would work for any kind of matrix?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by