Reshaping 4D Matrix, Keeping 4th Dimension Intact
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have had this question for a long ime and even though I THINK I know the answer, I'd like to get definitive confirmation. I work a lot with 4D fMRI matrices where the first three dimensions represent x,y,z coordinates for a spatial picture and the fourth dimension is a time component. For data manipulation I constantly need to load the original fMRI image and then reshape it to a 2D array with all the the first 3 spatial dimensions stacking row after row and the 4th time dimension being maintained. So e.g. point x,y,z = 1,1,1 would have (for example) 240 time points associated with it and then, after reshape, would be row 1 with the same 240 time points now in the columns.
Up to now I have been doing this with the command:
fMRI = reshape(4dmatrix, [], 240);
And then putting it back together with
final = reshape(fMRI, x, y,z, 240);
The visual results look correct so I've always just assumed this was right but I've never been 100% sure. 'Reshape' is easy enough to understand with 2D matrices but once I started using it with 3D(+) matrices I started getting confused about what exactly happens and I'd really like to get confirmation about my thinking.
Thank you. Lauren
2 comentarios
Mohammad Abouali
el 29 de Feb. de 2016
You are doing it fine. After the first command
fMRI(1,:) = 4dmatrix(1,1,1,:);
fMRI(2,:) = 4dmatrix(2,1,1,:);
...
fMRI(x,:) = 4dmatrix(x,1,1,:);
fMRI(x+1,:) = 4dmatrix(1,2,1,:);
and so on
Leonardo Tozzi
el 5 de Mayo de 2021
I have a follow-up on this. I have exactly the same goal, but I would like my original 4th dimension to be the rows of the final matrix, instead of the columns.
I could just transpose the result, but I wonder if there is an easier way using just the reshape command.
Thank you!
Respuestas (1)
Matthew Eicholtz
el 29 de Feb. de 2016
It seems like your code is doing what you expect. In particular, final and 4dmatrix should be equal in your example. If you want to get more insight into how the reshape function actually reshapes the data, try a simple example like this:
a = reshape(1:72,3,3,2,4); %creates a 3-by-3-by-2-by-4 matrix of integers from 1 to 72
b = reshape(a,[],4);
c = reshape(b,3,3,2,4); %convert back to same format as 'a'
assert(isequal(a,c));
Look at 'a' and 'b' in the Command Window and you'll see how the reshaped matrix is filled. In particular, reshape tries to fill the output matrix by traversing the first dimension of the input matrix (rows), then the second dimension (cols), then the third dimension, and so forth.
Hope this helps.
0 comentarios
Ver también
Categorías
Más información sobre Other Formats en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!