This is function to help

조회 수: 6 (최근 30일)
Master Blabla
Master Blabla 2020년 11월 5일
편집: Master Blabla 2020년 11월 17일
for j=1:500000
a = V(:,j);
Val(j) = SD(a,QuImage);
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 11월 5일
To vectorize the code, you vectorize SD -- which you do not show the code for.

댓글을 달려면 로그인하십시오.

답변 (2개)

Sindar
Sindar 2020년 11월 5일
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 2020년 11월 5일
Val = sum((VImage - QuImage).^2,1) .';
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 11월 5일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by