var2.m 390 B

1234567891011121314151617
  1. % V = VAR2(MTX,MEAN)
  2. %
  3. % Sample variance of a matrix.
  4. % Passing MEAN (optional) makes the calculation faster.
  5. function res = var2(mtx, mn)
  6. if (exist('mn') ~= 1)
  7. mn = mean2(mtx);
  8. end
  9. if (isreal(mtx))
  10. res = sum(sum(abs(mtx-mn).^2)) / max((prod(size(mtx)) - 1),1);
  11. else
  12. res = sum(sum(real(mtx-mn).^2)) + i*sum(sum(imag(mtx-mn).^2));
  13. res = res / max((prod(size(mtx)) - 1),1);
  14. end