No, your SD function uses sum(X(:).^2) which can only ever return a scalar value. You need one value per column. Your SD function is not compatible with vectorzing.
You could easily make an SD function that did the same thing my code does:
function [sd, X] = SD(I1, I2);
X = I1 - I2;
sd = sum(X.^2,1).';
end
and then you would
Val = SD(VImage, QuImage);
The code I posted uses "implicit expansion", which was added in R2016b.
Your VImage is an array 500 x 50. Your QuImage is an array 500 x 1. You want to subtract QuImage from each column of VImage, which could be done if you replicated QuImage to be 50 columns wide, like
VImage - repmat(QuImage, 1, size(VImage,2))
then you would be subtracting 500 x 50 and 500 x 50, getting a 500 x 50 result. Then you can square each entry using .^2 getting a 500 x 50 result. Then you add along the first dimension, getting a 1 x 50 result -- the sum of squares applied along each column.
It happens that MATLAB has built in detection if you have arithemetic operations between two matrices of different sizes, for the case where the places that the sizes are different, one of the two is length 1 there. So in the 500 x 50 minus 500 x 1 case, the place they disagree, 50 vs 1, the second of them has size 1. In such a case, MATLAB automatically acts as-if it replicated the data with repmat... except that in most cases it does not need to actually replicate the data and just manipulates what it is working with internally to give the result as-if the replication had occurred. Something like as-if it made copies of the pointers to the data instead of copying the data.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
1 Comment
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/637920-this-is-function-to-help#comment_1113475
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/637920-this-is-function-to-help#comment_1113475
Sign in to comment.