kashila
kashila

Reputation: 19

Calculate the variance of a 2D matrix matlab

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

Answers (3)

christosh
christosh

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

NoDataDumpNoContribution
NoDataDumpNoContribution

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

radhika
radhika

Reputation: 9

var1 = var(double(twoDarray(:)));

Upvotes: 0

Related Questions