how to find the number of 1 in a single column?
이전 댓글 표시
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
Azzi Abdelmalek
2012년 9월 5일
is it a string ?
Azzi Abdelmalek
2012년 9월 5일
what is the real question?
답변 (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
2012년 9월 5일
c='0 0 0 0 1 0 0 1 1 0 0 0 1 0'
sum(str2num(c))
Sivakumaran Chandrasekaran
2012년 9월 5일
Sivakumaran Chandrasekaran
2012년 9월 5일
0 개 추천
댓글 수: 2
Sivakumaran Chandrasekaran
2012년 9월 5일
José-Luis
2012년 9월 5일
your_result = find(ismember(your_matrix,[1 2],'rows'))';
Matt Tearle
2012년 9월 5일
편집: Matt Tearle
2012년 9월 5일
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))
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!