Calculate a mean if two statements are true

1 view (last 30 days)
I have a matrix that has several rows and columns of data. I am trying to get a mean from all the values of row 6 but only if row 5 = 1 and row 7 = 0. Initially to calculate mean of row 6 if row 5 = 1 I used this:
mean(cellm(6,logical(cellm(5,:))));
Now I would like to add the second portion where row 7 = 0. I have attempted this with the code below:
mean(cellm(6,(find(cellm(7,:) < 1) & (logical(cellm(5,:))))))
But I get the error "Inputs must have the same size.
Any suggestions would be appreciated.
Here is an example of the matrix below:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
4 0 0 0 0 4 4 0 0 0
11 11 11 11 11 22 22 11 11 11
0 1 1 1 1 1 1 1 1 1
1200 1700 1400 1800 1800 417 603 1700 1600 1700
0 1 1 1 1 0 0 1 1 1
1 1 1 1 1 2 2 1 1 1
1 0 0 0 0 0 0 0 0 0

Accepted Answer

Joseph Cheng
Joseph Cheng on 7 May 2015
you don't need find. you just need to use logical indexing through
mean(cellm(6,~cellm(7,:) & cellm(5,:)))
which will AND the row5 and the inverse of row7. it'll only do the columns in row 6 where there are ones.
[~cellm(7,:) & cellm(5,:))]=
0 0 0 0 0 1 1 0 0 0
cellm(5,:)=
0 1 1 1 1 0 0 1 1 1
cellm(7,:)=
0 1 1 1 1 1 1 1 1 1

More Answers (0)

Community Treasure Hunt

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

Start Hunting!