4
votes

So I have found many questions and answers about this on SO already and I think my approach should work as it is not very complicated. However I have tried every position for my time dimension possible and for me right now reshaping and keeping one dimension the same does not work:

I have a 400x400x20x24 array in which 400x400 is an image and 24 is a number of images at 20 times. I have to do an operation on each voxel and to make this quicker I want to reshape my array into a 2D matrix where one dimension is 20 and only has the time values. I know (or thought I knew) how to do this and have tried every possible order of dimensions prior to reshaping, but none result in my old data:

array1 = rand(400,400,20,24);

This is how one voxel ove time looks

plot([1:20], squeeze(array1(200,200,:,12)))

original

twoD1 = reshape(array1, [], 20);
size(twoD)
    ans = 3840000 20

so far so good until I plot a pixel and its time values

plot([1:20], squeeze(twoD1(962400,:)))

reshape1

Hmmm wait a minute the dimension of size 20 is no longer the original dimension of size 20, maybe rearranging my original dimensions will affect this.

array2 = permute(array1, [3 1 2 4]);
array3 = permute(array1, [1 3 2 4]);
array4 = permute(array1, [1 2 4 3]);

twoD2 = reshape(array2, [], 20);
twoD3 = reshape(array3, [], 20);
twoD4 = reshape(array4, [], 20);

plot([1:20], squeeze(twoD2(962400,:)))
plot([1:20], squeeze(twoD3(962400,:)))
plot([1:20], squeeze(twoD4(962400,:)))

reshape2

reshape3

reshape4

I dont understand why it doesnt work. Ive looked at these questions, but they seem to suggest I do it right, right?

Reshaping a matrix in matlab

How do I resize a matrix in MATLAB?

Reshaping a 3 dimensional array to 2 dimensions

Change a multidimensional vector to two dimensional vector matlab

MATLAB reshape matrix converting indices to row index

How to reshape matlab matrices for this example?

Reshape matrix from 3d to 2d keeping rows

Reshape 3d matrix to 2d matrix

Of course I also read:

http://www.mathworks.nl/help/matlab/ref/reshape.html

http://www.mathworks.nl/help/matlab/ref/permute.html

All to no avail. Someone please help me? Thank you!

1
Did you expect to see exactly the same plot as the first one? But in the first plot, you look at the central pixel of image 12, how do you make sure that you are looking at the same pixel of the same image in any of the last 4 plots?Bas Swinckels
This is just a matter of book keeping. I've updated and fixed my answer -- I think it does what you want.chappjc
@BasSwinckels Well visually all pixels look the same, they are all exponential decays. Thats why I can vsually check if my t dimension is still the samen. To make sure I get the right pixel sub2ind can be used as chappjc showed.Leo
That information was missing, since you used rand in your example.Bas Swinckels

1 Answers

4
votes

The first issue to note is that twoD1 = reshape(array1, [], 20); does not do what you want since array1 is 400x400x20x24. The reshape only works as intended if the last dimension is 20:

twoD = reshape(permute(array1,[1 2 4 3]),[],20);

That gives you all pixels, all 24 images by 20 time points. If you want to plot the 20 time points for pixel (200,200) for image 12, do the following:

[numRows,numCols,numTimes,numSlices] = size(array1);
imgInd = 12; pixRow = 200; pixCol = 200;
ind = sub2ind([numRows numCols numSlices],pixRow,pixCol,imgInd)
% ind = pixRow + numRows*(pixCol-1) + numRows*numCols*(imgInd-1)
plot(1:size(twoD,2), twoD(ind,:))

EDIT: Sorry, I computed ind wrong first. Works now.