How to do the mean with a condition?

Hello,
Here is my problem, i have a huge matrix. And i have to do the mean for a row but only with a condition, i created a logical vector which contain 1 when i want to include the value and 0 when i don't.
For example
A=
[2 3 4 5 0
6 7 8 9 1
7 5 1 2 1
2 5 6 9 0]
And i want the mean for the second column when the 4th have a value of 1, so mean=6.
Thanks for your help :)

4 Comments

How did you create the logical vector that says whether or not to get the mean of that row?
And I read your last sentence about the second column numerous times and still can't figure out what it means. When the 4th WHAT has a value of 1? When an element in the 4th column has a value of 1 - but it never has a value of 1, only 5,9,2, and 9. What was averaged to get the 6???
I had a vector with 0,1,2 and 3 which are parameters and i did : complete_data(:,137)=complete_data(:,136)==1;
Sorry it was the 5th column, so it was a(2,2)=7 and a(3,2)=5
I guess I'll leave it to someone who is still not confused by what you're saying to answer.
Sorry, english isn't my mother tongue.
I find a solution with a loop, not very elegant but fine for the moment.
Thanks for your time.

Sign in to comment.

Answers (1)

Matt J
Matt J on 4 Nov 2013
Edited: Matt J on 4 Nov 2013
mean(A(logicalVector,:),1)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 4 Nov 2013

Edited:

on 4 Nov 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!