I am taking one number and converting it into binary and checking each bit whether it is 1 and if it is 1 then is added to next bit which is 1 and result is stored in c.
clc;
q=347;
q=dec2bin(q)
n=size(q,2)
c='0'
for i=1:n
if q(i)==1
c=c+q(i)
end
end

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 12월 27일
편집: Azzi Abdelmalek 2013년 12월 27일
Maybe you want
q=347;
q=dec2bin(q)
c=sum(q=='1')
%or
c=numel(strrep(q,'0',''))

추가 답변 (1개)

Amit
Amit 2013년 12월 27일

0 개 추천

The question is unclear. Can you elaborate?
One issue I see is that q(i) == '1' and not q(i) == 1. As q is a string and not an integer.

카테고리

도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2013년 12월 27일

편집:

2013년 12월 27일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by