MATLAB Answers

0

Convert unfolded tensor back to higher dimensional tensor?

Asked by Rohit Gupta on 5 Jun 2018
Latest activity Commented on by snowflake on 4 Dec 2018
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

  0 Comments

Sign in to comment.

Products


Release

R2018a

1 Answer

Answer by Aakash Deep on 5 Jun 2018

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 Comments

Tensor: 12 x 50 x 40
Unfolded tensor: 50 x 480 (Mode-2 Unfolding)
But i want to know if there is a method for any given dimension tensor given that matrix is a proper Mode-n Unfolded.
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 :)
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?

Sign in to comment.