Reputation: 6290
I have a set of 4D matrices in Matlab. The first three dimensions are spatial, the last is temporal. That means each voxel is a time series. If Img is the 4D image, Img(x,y,z,:) is the time series of voxel with coordinate (x,y,z).
Now I want to normalize (zero mean and unit variance) each time series of the image. How can I do this? Perhaps with zscore(Img,[],4)
?
Second, I want to concatenate the time series of all images. Let's say Img1, Img2, Img3,....Imgn are the 4D images. For each voxel I want to concatenate the time series over all images so that in the end I have one 4D image with the same spatial but extended temporal dimension. So let's say the voxel has the coordinate (x,y,z), then the concatenated time series would be [squeeze(Img1(x,y,z,:)); squeeze(Img2(x,y,z,:));....;squeeze(Imgn(x,y,z,:))]
. This should be done for all voxels.
How can I do this? It could be done with for loops but this is not efficient.
Upvotes: 2
Views: 592
Reputation: 112759
This can be easily done with bsxfun
:
Img = bsxfun(@rdivide, Img, std(Img,0,4)); %// unit variance. Or change 0 to 1; see std
Img = bsxfun(@minus, Img, mean(Img,4)); %// zero mean
Just use cat
along the fourth dimension:
result = cat(4, Img1, Img2, Img3);
It's easier if you have all images in a cell array:
cellImgs = {Img1, Img2, Img3};
because then you can use a comma-separated list
result = cat(4, cellImgs{:});
so the code is independent of the number of images.
Upvotes: 5