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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With