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!