how to find the number of 1 in a single column?
Mostrar comentarios más antiguos
My input is
0 0 0 0 1 0 0 1 1 0 0 0 1 0
I should find how many 1 is present
2 comentarios
Azzi Abdelmalek
el 5 de Sept. de 2012
is it a string ?
Azzi Abdelmalek
el 5 de Sept. de 2012
what is the real question?
Respuestas (6)
If it is a string:
c = '0 0 0 0 1 0 0 1 1 0 0 0 1 0';
s = sum(c == '1');
% or:
s = length(strfind(c, '1');
if it is a numeric vector:
c = [0 0 0 0 1 0 0 1 1 0 0 0 1 0];
s = sum(c); % as Jose-Luis has suggested already
% or:
s = nnz(c);
your_answer = sum(your_input)
Azzi Abdelmalek
el 5 de Sept. de 2012
c='0 0 0 0 1 0 0 1 1 0 0 0 1 0'
sum(str2num(c))
Sivakumaran Chandrasekaran
el 5 de Sept. de 2012
Sivakumaran Chandrasekaran
el 5 de Sept. de 2012
0 votos
2 comentarios
Sivakumaran Chandrasekaran
el 5 de Sept. de 2012
José-Luis
el 5 de Sept. de 2012
your_result = find(ismember(your_matrix,[1 2],'rows'))';
Matt Tearle
el 5 de Sept. de 2012
Editada: Matt Tearle
el 5 de Sept. de 2012
idx = find(all(bsxfun(@eq,x,y),2));
where x is your matrix and y is your test vector (eg y = x(1,:), in your example). For example
x = randi(4,1000,5);
y = x(3,:);
idx = find(all(bsxfun(@eq,x,y),2))
Categorías
Más información sobre Resizing and Reshaping Matrices en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!