how to search about a matrix in a cell?

조회 수: 3 (최근 30일)
Sarah A
Sarah A 2019년 1월 19일
댓글: Sarah A 2019년 1월 19일
Hello,
Suppose that I have the cell matrix A which contains the following elements:
A={
[0;0] [0;1]
[1;0] [0;0]
}
So I want to use a line of code that can count how many [0;0] are in each row ?
Regards,

채택된 답변

Guillaume
Guillaume 2019년 1월 19일
편집: Guillaume 2019년 1월 19일
it's always a good idea to give the result you expect for your example to avoid ambiguity.
A={
[0;0] [0;1]
[1;0] [0;0]
}
sum(cellfun(@(x) isequal(x, [0;0]), A), 2)
results in
ans =
1
1
count of [0;0] in each row.
  댓글 수: 1
Sarah A
Sarah A 2019년 1월 19일
Great ! Thank you for your answer.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 1월 19일
편집: madhan ravi 2019년 1월 19일
Sarah's (OP) example:
A={ ...
[0;0] [0;1]
[1;0] [0;0]
};
counts=nnz(~cellfun(@any,A))
Gives:
counts =
2
Stephen's example:
A={[0;0] [0;1] [1;0] [0;0] [0;0]};
counts=nnz(~cellfun(@any,A))
Gives:
counts =
3
  댓글 수: 5
madhan ravi
madhan ravi 2019년 1월 19일
check the edited answer
Sarah A
Sarah A 2019년 1월 19일
Yes this code work with me:
A={ ...
[0;0] [0;1]
[1;0] [0;0]
};
counts=nnz(~cellfun(@any,A))
but what if I want to count how many [0;0] in each row? how many [0;1] in each row? how many [1;1] in each row?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by