user1036959
user1036959

Reputation: 1

translating complicated matlab formula

I'm a newbie in matlab. But I do have knowledge in coding c++, java and all. But, do keep in mind that I'm no professional in those either. Just a beginner.

So..I'm workin on a matlab-based system. And I'm kinda stuck on how to write complicated formulas in matlab. I already have the algorithms provided, just I don't really know how to write it in matlab. This system is associated with image recognition.

*μ= 1/MN ∑(i=1)^M▒∑(j=1)^N▒Pij* (I copied this straight from MsWord since I cant post images yet)

I would really appreciate help. Thanks in advance.

Upvotes: 0

Views: 138

Answers (2)

arne.b
arne.b

Reputation: 4330

It seems to me you just want to average all values in a matrix P.

To do literally what is in your formula, you could use

mu = 1/(size(P,1)*size(P,2)) *sum(sum(P));

For any matrix P, size(P,dim) returns its size along the specified dimension, i.e. your M or N for dim=1 or 2. For matrices, sum will return a vector of the sum of the values each column of the matrix, sum applied to a vector returns the sum of all its elements.

However, the same can be achieved more easily:

mu = mean(P(:));

where P(:) is a P regarded as a single column. mean(P) would again calculate the mean of every column of P (thus, mean(mean(P)) is another way to arrive at the mean of all elements of P).

Edit: If M and N are not the size of P along the given dimension, i.e. if you only want to consider the first M rows and the first N columns, use P(1:M,1:N) to refer to the relevant sub-matrix.

Upvotes: 1

Emilio M Bumachar
Emilio M Bumachar

Reputation: 2613

Use for loops for the sums and products (no pun intended). Type "help for" in the prompt for the synthax.

Use variables to hold the results of these sums and products and, optionally, other separable sub-expressions.

Upvotes: 0

Related Questions