Asked by Sivakumaran Chandrasekaran
on 5 Sep 2012

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

Answer by Jan Simon
on 5 Sep 2012

Edited by Jan Simon
on 5 Sep 2012

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);

Log in to comment.

Answer by Azzi Abdelmalek
on 5 Sep 2012

c='0 0 0 0 1 0 0 1 1 0 0 0 1 0' sum(str2num(c))

Log in to comment.

Answer by Sivakumaran Chandrasekaran
on 5 Sep 2012

Thanks for all replies...

1 1

1 2 3 1 2 1 3 1 1 2 3 2 2 1 3 1 2 1 3 2 2 2 3 1 2 2

Log in to comment.

Answer by Sivakumaran Chandrasekaran
on 5 Sep 2012

thats my input matrix.. let me take query input as 1 1... now i have to check this 1 1 with all the rows of the given input matrix.. I have find the rows which matches this input.

Sivakumaran Chandrasekaran
on 5 Sep 2012

José-Luis
on 5 Sep 2012

your_result = find(ismember(your_matrix,[1 2],'rows'))';

Log in to comment.

Answer by Matt Tearle
on 5 Sep 2012

Edited by Matt Tearle
on 5 Sep 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))

Log in to comment.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 2 Comments

## Azzi Abdelmalek (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/47433-how-to-find-the-number-of-1-in-a-single-column#comment_97636

is it a string ?

## Azzi Abdelmalek (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/47433-how-to-find-the-number-of-1-in-a-single-column#comment_97643

what is the real question?

Log in to comment.