How to do the mean with a condition?
Show older comments
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
Image Analyst
on 4 Nov 2013
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???
ProjectVOC
on 4 Nov 2013
Image Analyst
on 4 Nov 2013
I guess I'll leave it to someone who is still not confused by what you're saying to answer.
ProjectVOC
on 4 Nov 2013
Answers (1)
mean(A(logicalVector,:),1)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!