# Convert unfolded tensor back to higher dimensional tensor?

24 views (last 30 days)
Rohit Gupta on 5 Jun 2018
Commented: mona ismail on 8 May 2020
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

#### 1 Comment

mona ismail on 8 May 2020
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?

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 :)

Rohit Gupta on 6 Jun 2018
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.
Aakash Deep on 6 Jun 2018
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 on 4 Dec 2018
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?

R2018a

### Community Treasure Hunt

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

Start Hunting!