フィルターのクリア

Isolate Digits in a number

1 回表示 (過去 30 日間)
Raviteja
Raviteja 2011 年 4 月 7 日
Take c input as
c= 0010100101100
Note: Please don't take c as char
isolate each digit into index of "nary"
i.e.,
>> naray =
0 0 1 0 1 0 0 1 0 1 1 0 0
i.e.,
nary(1)=0
nary(2)=0
nary(3)=1
nary(4)=0
nary(5)=1
nary(6)=0
nary(7)=0
nary(8)=1
nary(9)=0
nary(10)=1
nary(11)=1
nary(12)=0
nary(13)=0
  1 件のコメント
David Young
David Young 2011 年 4 月 7 日
Could you say why you want to do this, please? I ask because it looks a bit like it could be a homework problem. Also, is it assumed that nary is to have 13 elements? The number of leading zeros in 0010100101100 is lost, of course, so there's no information in c about how many leading zeros are to be stored in nary.

サインインしてコメントする。

採用された回答

Matt Fig
Matt Fig 2011 年 4 月 7 日
If c is not a char, then what is it? How could you even enter c in MATLAB if it is not a char? You will lose the leading zeros if c is not a char.
>> c = 0010100101100
c =
10100101100

その他の回答 (3 件)

Paulo Silva
Paulo Silva 2011 年 4 月 7 日
So if c isn't char it could be a vector:
nary='0010100101100' - '0'; %create the vector with the data provided
%now you can call each value from 1 until 13
nary(1)
nary(2)
nary(3)
nary(4)
nary(5)
nary(6)
nary(7)
nary(8)
nary(9)
nary(10)
nary(11)
nary(12)
nary(13)
  1 件のコメント
Raviteja
Raviteja 2011 年 4 月 7 日
Your answer working! Thanks!

サインインしてコメントする。


David Young
David Young 2011 年 4 月 7 日
Well, since Matt's answer has been accepted (!), I guess I might as well offer mine, for non-char input:
nary = floor(c*10.^(-13:0))-10*floor(c*10.^(-14:-1));
  2 件のコメント
Walter Roberson
Walter Roberson 2011 年 4 月 7 日
Ah, but it doesn't handle negative binary numbers ;-)
If you change the floor() to fix() then for negative c, each of the 1 values will come out negative, which would arguably be a fair solution.
David Young
David Young 2011 年 4 月 11 日
Indeed!

サインインしてコメントする。


Walter Roberson
Walter Roberson 2011 年 4 月 7 日
nary = sprintf('%d',c) - '0';
Subject, though, to the problems the others noted about losing leading zeros.
Note: this solution is also not valid if there are more than 53 bits from the first 1 in the number.

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by