MATLAB Answers

how to search about a matrix in a cell?

1 view (last 30 days)
Sarah A
Sarah A on 19 Jan 2019
Commented: Sarah A on 19 Jan 2019
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,

  0 Comments

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 19 Jan 2019
Edited: Guillaume on 19 Jan 2019
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 Comment

Sarah A
Sarah A on 19 Jan 2019
Great ! Thank you for your answer.

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 19 Jan 2019
Edited: madhan ravi on 19 Jan 2019
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 Comments

Show 2 older comments
Sarah A
Sarah A on 19 Jan 2019
thank you for your answer but this is not what I need. what if I want to count how many [0;1] there ?
Also noticed that in my question I wrote each two matrices in a row thats mean A is (2*2 matrix)
Sarah A
Sarah A on 19 Jan 2019
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?

Sign in to comment.

Sign in to answer this question.


Translated by