MATLAB Answers

This is function to help

1 view (last 30 days)
Master Blabla
Master Blabla on 5 Nov 2020
Edited: Master Blabla on 17 Nov 2020
for j=1:500000
a = V(:,j);
Val(j) = SD(a,QuImage);
end
  1 Comment
Walter Roberson
Walter Roberson on 5 Nov 2020
To vectorize the code, you vectorize SD -- which you do not show the code for.

Sign in to comment.

Answers (2)

Sindar
Sindar on 5 Nov 2020
Whether this can be vectorized is really a question about SD. It's possible it works on columns straight off:
Val = SD(VImage,QuImage);
or maybe you need to create a variant:
Val = SD_col(VImage,QuImage);
% or add an argument specifying the dimension
Val = SD(VImage,QuImage,2);

Walter Roberson
Walter Roberson on 5 Nov 2020
Val = sum((VImage - QuImage).^2,1) .';
  1 Comment
Walter Roberson
Walter Roberson on 5 Nov 2020
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.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by