C-order reshape of multi-dimensional array in matlab

Hello,
I have a question regarding reshape in matlab, it seems that matlab reshapes N-dimensional array according to Fortran-order, however, I would like a C-order reshape, that is a line-wise reshape.
Suppose now there is a matrix M with 4 rows and 4 columns as follows:
M = [1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16]
Then M is going to be reshaped to a 3D array with dimension (2,2,4) shown in the picture below. Does anyone know how to do this in a generalized way (suitable for even higher dimension)?
Thanks!
reshape_problem_2.jpg

 Akzeptierte Antwort

Jan
Jan am 10 Mär. 2019
Bearbeitet: Jan am 10 Mär. 2019
Reshaping in line-wise order is not meaningful, because reshaping means to keep the order of elements in the memory and the order is defined columnwise in Matlab. But you can simply change the order by permute:
x = rand(2,3,4);
y = permute(x, [2, 1, 3:ndims(x)]);
result = reshape(y, [whateverYouWant])

7 Kommentare

Yijun Li
Yijun Li am 12 Mär. 2019
Hello Jan,
Thanks for your reply, however I found it a bit difficult to follow it and solve my problem. I have edited the question and could you please explain how to apply your method to the new example that I provide.
Thx!
@Yijun Li: I do not understand your example. The screen is nice, but valid Matlab syntax would be better. As far as I can see, you can run my example directly with your data:
Maybe your data are:
X = reshape(1:16, 2, 2, 4);
If you want to store the elements in row-wise order, simply swap the first two dimensions:
y = permute(x, [2, 1, 3]);
In the general case of n dimensions:
y = permute(x, [2, 1, 3:n]);
Now the data are store in row-wise order and you can reshape them as you need.
@Jan
Hello Jan,
Unfortunately your solution fails the mission, maybe I have not explained my problem well, but anyway I have solved the problem in a generalized way with your insight.
Let me explain my solution according to the example specified in my problem:
Suppose again matrix M, which is going to be reshaped to a 3D array with dimension (2,2,4) shown in the picture above.
M = [1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16]
As matrix M is arranged in C-order (line-wise), first transpose data to Fortran-order (column-wise).
data = M.'
Then specify the dimension along each axis from low to high (3rd to 1st).
dimension_low2high = [4,2,2]
Next reshape the data in Fortran-order (column-wise) from low-dimension to high dimension.
ndarray = reshape(data,dimension_low2high)
Finally swap the position of low dimension and high dimension to meet C-order.
ndarray = permute(ndarray,[ndims(ndarray):-1:1])
Then have a check whether the new indexing is correct, according to this example, the following code block will show integers from 1 to 16.
for i1=1:2
for i2=1:2
for i3=1:4
ndarray(i1,i2,i3)
end
end
end
@Yijun Li: Your code:
M = [1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16];
data = M.';
dimension_low2high = [4,2,2];
ndarray = reshape(data,dimension_low2high);
ndarray = permute(ndarray,[ndims(ndarray):-1:1]);
creates exactly the same output as my suggested code:
X = reshape(M, 2, 2, 4);
Y = permute(X, [2, 1, 3]);
isequal(ndarray, Y)
>> TRUE
Therefore I do not understand, why my code "fails the mission".
Hi just wondering what is 2,1,3 stand for in this line < permute(x, [2, 1, 3:ndims(x)]>
is it always being 2,1,3?
Thanks
That's the mapping between the dimensions of your original array and the dimensions of the result.
A = reshape(1:24, [4 3 2]);
szA = size(A)
szA = 1×3
4 3 2
p = [2 1 3];
B = permute(A, p);
szB = size(B)
szB = 1×3
3 4 2
A is size 4 in its first dimension. Because the first element of p is 2, B is size 4 in its 2nd dimension.
A is size 3 in its second dimension. Because the second element of p is 1, B is size 3 in its 1st dimension.
I was very careful to distinguish first and second from 1st and 2nd in those descriptions.
"Therefore I do not understand, why my code "fails the mission"."
I accepted your answer, Jan, because it is clear that permuting the first two dimensions is the general solution.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2018a

Gefragt:

am 10 Mär. 2019

Kommentiert:

am 7 Sep. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by