简体   繁体   中英

How do I weigh images and then add them up efficiently in MATLAB?

I need a kind of volume-vector 'dot product' of sorts. Here is the problem:

I have a 2x3x4 volume, which is a nothing but a bunch of 4 2x3 images one after the other. Let's call it volume vol .

I also have a 4x1 vector, and let us call it vec .

I want my output to be a weighted sum of each image. Thus my output will be a 2x3 image. It will be formed by taking:

output = vol(:,:,1).*vec(1) + vol(:,:,2).*vec(2) + vol(:,:,3).*vec(3) + vol(:,:,4).*vec(4)

I can put this in a for-loop, but I was wondering if there was an easier way of doing it.

Thanks.

Perfect case for bsxfun after re-arranging dimensions of vec with permute to let vec expand onto the size of vol and let elementwise multiplication happen and finally use sum to fetch the desired result -

sum(bsxfun(@times,vol,permute(vec,[3 2 1])),3)

Or use matrix multiplication with some reshape 's -

reshape(reshape(vol,[],numel(vec))*vec,size(vol,1),[])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM