Borrar filtros
Borrar filtros

Matrix Algebra without Loop

5 visualizaciones (últimos 30 días)
Angelo (Scotty) Gilmore
Angelo (Scotty) Gilmore el 28 de Jun. de 2017
Comentada: Angelo (Scotty) Gilmore el 28 de Jun. de 2017
I'm trying to do a relatively simple calculation, but my linear algebra is a bit rusty, and I'd like to know if it can be done without a loop. I'm taking a 3D image stack and I want to subtract the image frame average from each pixel in that frame to correct the variance for irradiance variation between frames during the collection. There is slightly more to the formula than what I show, but this is the only part in which I can't figure out how to avoid a loop.
One thought I had was to take the frame average (1D vector) and convert into a 3D matrix with each 2D frame repeating a single value from the 1D frame average to fill the matrix so that I could subtract one matrix directly from the other and square the result. This seems cumbersome though.
Thanks in advance for any elegant solutions!
imgVarCorrectedSum = zeros(nRow,nCol);
for i= 1:nFrames
imgVarCorrectedSum(:,:) = imgVarCorrectedSum(:,:) + ...
( imgActive(:,:,i) - imgAvgFrame(i) ).^2;
end

Respuesta aceptada

dpb
dpb el 28 de Jun. de 2017
With recent release (R2016b?) there's automagic expansion that'll probably do what you want....prior to that, bsxfun does such singleton expansion.
Small sample--
>> IA=rand(3,3,3);
>> IAavg=mean(mean(IA));
>> bsxfun(@minus,IA,IAavg)
ans(:,:,1) =
-0.3818 0.5020 0.3574
-0.0173 -0.4553 0.4088
-0.3533 0.3150 -0.3755
ans(:,:,2) =
0.0077 0.0393 -0.1283
-0.1322 0.5185 -0.2466
0.4080 -0.2103 -0.2560
ans(:,:,3) =
0.3265 -0.3978 -0.1918
0.0369 0.3103 -0.0295
0.0071 0.0793 -0.1410
>>
Check get expected result for first plane...
>> IA(:,:,1)-IAavg(1)
ans =
-0.3818 0.5020 0.3574
-0.0173 -0.4553 0.4088
-0.3533 0.3150 -0.3755
>>
If you haven't already recognized, note the mean vector calculation is really pretty clever by using the default behavior of Matlab to work on the first array dimension by default.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by