MATLAB: Large 3D Matrix calculation

for loopmatrix calculation

Hi All I have two large matrix and I want to calculate an expression without for loop. this expression is something like matrix d as below
f=[f1 f2 f3]
r=[r1 r2 r3]
d=[f1-r1 f2-r1 f3-r1
f1-r2 f2-r2 f3-r2
f1-r3 f2-r2 f3-r3]
can any one help me?! consider that size(f)=1*1*400 and size(r)=50*50*900 and fii=f(1,1,ii) and rjj=r(:,:,jj)

Best Answer

You gave this code in a comment to my other answer:
size(f)==[1,350]
size(r)==[50,50,900]
for ii=1:350
for jj=1:900
s(ii,jj) = sum(sum(sum( repmat(f(ii),[50,50,1]) - r(:,:,jj) ))) ;
end
end
You can try this instead:
A = 50*50*reshape(f,1,[]);
B = reshape(sum(sum(r,1),2),[],1);
C = bsxfun(@minus,A,B);
It produces the same result as your nested loops.
Related Question