Calculate a mean if two statements are true

2 ビュー (過去 30 日間)
Daniel
Daniel 2015 年 5 月 7 日
コメント済み: Daniel 2015 年 5 月 7 日
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

採用された回答

Joseph Cheng
Joseph Cheng 2015 年 5 月 7 日
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
  1 件のコメント
Daniel
Daniel 2015 年 5 月 7 日
Fantastic, thank you very much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by