how to search about a matrix in a cell?

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,

 Risposta accettata

Guillaume
Guillaume il 19 Gen 2019
Modificato: Guillaume il 19 Gen 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.

Più risposte (1)

madhan ravi
madhan ravi il 19 Gen 2019
Modificato: madhan ravi il 19 Gen 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 Commenti

Stephan
Stephan il 19 Gen 2019
Modificato: Stephan il 19 Gen 2019
consider:
A={[0;0] [0;1] [1;0] [0;0] [0;0]}
isnt your code doing the opposite of what the op asked for? i would expect a result of 3 for this case.
See edited answer.
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)
check the edited answer
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?

Accedi per commentare.

Categorie

Scopri di più su Operators and Elementary Operations in Centro assistenza e File Exchange

Richiesto:

il 19 Gen 2019

Commentato:

il 19 Gen 2019

Community Treasure Hunt

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

Start Hunting!

Translated by