Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I efficently use Matlab to find the all the variances along the third dimension of a 3D array?

Tags:

matlab

If I have a matrix A of size nx * ny * nz, I could find the variances I'm looking for by using a double for loop:

varA = zeros(ny,nx);
for jj = 1:ny
  for ii = 1:nx
    varA(jj,ii) = var(A(jj,ii,:));
  end
end

However, I would very much like to avoid using this loop, as it can take a long time for large arrays. Is there an easy way to do this calculation efficiently in Matlab?

like image 698
Paulithan Avatar asked Nov 23 '25 21:11

Paulithan


1 Answers

You want to provide the dim input to var to specify the dimension along which to apply the calculation.

varA = var(A, 0, 3);

You must specify the weighting scheme (the second argument) to be the default (0).

NOTE: This dimension parameter is available for many simple calculations including mean, std, diff. Check the documentation for the specific function.

like image 54
Suever Avatar answered Nov 26 '25 23:11

Suever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!