问题描述
我有以下形式的函数:
function Out = DecideIfAPixelIsWithinAnEllipsoidalClass(pixel,means,VarianceCovarianceMatrix)
ellipsoid = (pixel-means)'*(VarianceCovarianceMatrix^(-1))*(pixel-means);
if ellipsoid <= 1
Out = 1;
else
Out = 0;
end
end
我正在使用matlab进行遥感过程想要对LandSatTM图像进行分类。这张图片有7个波段,是2048 * 2048.所以我将它们存储在3个2068 * 2048 * 7矩阵中。这个函数意味着先前使用类的样本计算的7 * 1矩阵在一个名为ExtractStatisticalParameters和VarianceCovarianceMatrix的函数中,你看到的是一个7 * 7矩阵:
I am doing remote-sensing processes with matlab and I want to classify a LandSatTM images.This picture has 7 bands and is 2048*2048.So I stored them in 3 dimentinal 2048*2048*7 matrix.in this function means is a 7*1 matrix calculated earlier using the sample of the class in a function named ExtractStatisticalParameters and VarianceCovarianceMatrix is a 7*7 matrix in fact you see that:
ellipsoid = (pixel-means)'*(VarianceCovarianceMatrix^(-1))*(pixel-means);
是椭圆体的等式。我的问题是每次你可以传递一个像素(它是一个7 * 1向量,其中每一行是分隔波段中像素的值),因此我需要写一个这样的循环:
is the equation of an ellipsoid.My problem is that each time you can pass a single pixel(it is a 7*1 vector where each row is the value of the pixel in a seperated band) to this function so I need to write a loop like this:
for k1=1:2048
for k2=1:2048
pixel(:,1)=image(k1,k2,:);
Out = DecideIfAPixelIsWithinAnEllipsoidalClass(pixel,means,VarianceCovarianceMatrix);
end
end
你知道它需要很多时间和精力系统。你建议我一种减少系统压力的方法吗?
and you know it will take alot of time and energy of the system.Can you suggest me a way to reduce the pressure applied on the systam?
推荐答案
无需循环!
pMinusMean = bsxfun( @minus, reshape( image, [], 7 ), means' ); %//' subtract means from all pixes
iCv = inv( arianceCovarianceMatrix );
ell = sum( (pMinusMean * iCv ) .* pminusMean, 2 ); % note the .* the second time!
Out = reshape( ell <= 1, size(image(:,:,1)) ); % out is 2048-by-2048 logical image
更新:
在下面评论中的(有点激烈的)辩论之后,我添加了一个由:
pMinusMean = bsxfun( @minus, reshape( image, [], 7 ), means' ); %//' subtract means from all pixes
ell = sum( (pMinusMean / varianceCovarianceMatrix ) .* pminusMean, 2 ); % note the .* the second time!
Out = reshape( ell <= 1, size(image(:,:,1)) );
此更改的关键问题是Matlab的 inv()
使用和(运营商 /
和 \
)而不是。
The key issue in this change is that Matlab's inv()
is poorly implemented and it is best to use mldivide
and mrdivide
(operators /
and \
) instead.
这篇关于Matlab:如何在2D矢量集上矢量化嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!