Reputation: 19
How do I make a function that calculates the variance of a 2D matrix using loops i and j...where do I start?
Upvotes: 1
Views: 14207
Reputation: 193
You could use reshape in order to transform image as a vector. After that, you could calculate the variance using:
v1 = var(reshape(im2(:,:),[],1));
Upvotes: 0
Reputation: 10859
You would not use loop variables in Matlab since this is inevitably much slower than using inbuilt (vectorized) functions.
The function var
calculates the variance of a matrix column-wise. With (:)
you convert a 2D matrix into a single column.
Or as Jonas pointed out, use:
var(array(:))
Upvotes: 1