Since you are performing only basic operations, like -, and .*it needs to be addressed with the help of bsxfun.
Use
bsxfun(@minus,P,S)
do an elementary subtraction P(k,1) - S(1,i). The output will be a matrix [2000,400]. You can apply the operation max(0,...)to this matrix and finally use it again bsxfunto multiply each row by the corresponding one r:
bsxfun(@times,max(bsxfun(@minus,P,S),0),r)
c [400,2000], , .
c = bsxfun(@times,max(bsxfun(@minus,P,S),0),r).';
: for
Elapsed time is 0.688408 seconds.
bsxfun
Elapsed time is 0.007884 seconds.
87 .