I have a string S='010101' I need to take each element from the string and check whether it 1,if it is one then the count is incremented by one in matlab?
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a string S='010101' I need to take each element from the string and check whether it 1,if it is one then the count is incremented by one in matlab?
I have used the following code,but the value of count is not changing.
St='010101';
count=0;
a=0;
for i=1:6
a=St(i);
if(a==1)
count=count+1;
end
end
채택된 답변
Azzi Abdelmalek
2015년 7월 7일
편집: Azzi Abdelmalek
2015년 7월 7일
S='010101'
out=nnz(S-'0')
To correct your for loop
St='010101';
count=0;
a=0;
for i=1:6
a=St(i);
if(a=='1')
count=count+1;
end
end
댓글 수: 0
추가 답변 (1개)
Debarati Banerjee
2015년 7월 7일
This seems to work:
St='010101';
count=0;
a=0;
for i=1:6
a=St(1,i);
if(str2num(a)==1)
count=count+1;
end
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!