フィルターのクリア

How do I extract a difit from a binary string

5 ビュー (過去 30 日間)
Robert Jones
Robert Jones 2024 年 2 月 25 日
コメント済み: Star Strider 2024 年 2 月 25 日
Hello,
I need to represent a decimal by a binary string and then extract in a loop one the digit at a time.
Something like
A=45;
B=dec2bin(A):
lb=Length(B);
C=strings(lb,1);
D=convertCharsToStrings(B);
for i=1:lb
C=substr(D,i,1) % here I need to extract the digit. the line is just pseudo code
end
The line in the loop is not working.Any ideas?
Thank you

採用された回答

Star Strider
Star Strider 2024 年 2 月 25 日
Tweaking your code —
A=45;
B=dec2bin(A)
B = '101101'
lb=length(B);
C=strings(lb,1);
% D=convertCharsToStrings(B);
for i=1:lb
C(i)=B(i); % here I need to extract the digit. the line is just pseudo code
end
C
C = 6×1 string array
"1" "0" "1" "1" "0" "1"
Character arrays and string variables behave differently. The ‘D’ variable will not work the way you want it to in this context.
.
  2 件のコメント
Robert Jones
Robert Jones 2024 年 2 月 25 日
Great, Thank youi
Star Strider
Star Strider 2024 年 2 月 25 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by