Asked by AP
on 11 Feb 2013

Dear All,

I have a 4D array V, with the size [144 144 12 25]. In fact, it is a 4D medical image with 12 stacked slices in 25 different time. I want to apply a mask, named M, to each image in the array in a vectorized way. M is a 144x144 array containing logical values. Could someone help me how to do it?

Thanks,

Ahmad

*No products are associated with this question.*

Answer by José-Luis
on 11 Feb 2013

Edited by José-Luis
on 12 Feb 2013

Accepted answer

data = rand(144,144,12,25); your_mask = rand(144,144) > 0.5;

Assuming you want to multiply by the mask:

your_result = bsxfun(@times,data,mask);

Note that you can do operations other than multiplication. Please have a look at

doc bsxfun

Answer by Conrad
on 11 Feb 2013

Something like this:

fV = V.*M(:,:,ones(1,size(V,3)),ones(1,size(V,4)));

Conrad

Opportunities for recent engineering grads.

## 1 Comment

## José-Luis (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/63002#comment_128651

I notice that you don't seem accept many answers after asking questions. Some of the answers seemed valid to me. Please remember to accept answers if they help you, since it is the only "payment" the volunteers in this forum receive.