Mean of values in the 1st column where the value in the second column is zero

1 view (last 30 days)
I have data in a MAT file with 2 columns, the first column is length and the second column is 0 or 1.
eg: Length Y/N
2.5 1
3.2 0
How do I get mean of all values of first column where the value in second column is zero?

Answers (3)

Azzi Abdelmalek
Azzi Abdelmalek on 28 Feb 2014
Edited: Azzi Abdelmalek on 28 Feb 2014
M=[2.5 1;3.2 0]
c1=M(:,1)
c2=M(:,2)
idx=c2==0
out=mean(c1(idx))

Star Strider
Star Strider on 1 Mar 2014
In one line:
S = mean(M(M(:,2)==0),1)

Sid
Sid on 1 Mar 2014
Thanks,that will do.

Categories

Find more on Data Types in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!