combining two binary numbers

27 ビュー (過去 30 日間)
PRAVEEN GUPTA
PRAVEEN GUPTA 2020 年 3 月 2 日
回答済み: Shubham 2024 年 8 月 29 日
i have two binary numbers a=[1100] and b=[010] i have to form c=[0101100] please suggest me how to do this.
Thanks
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 3 月 2 日

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

回答 (1 件)

Shubham
Shubham 2024 年 8 月 29 日
Hi Praveen,
To concatenate two binary numbers in MATLAB, you can treat them as strings and concatenate them directly. Here's how you can achieve this:
a = '1100'; % Binary number a as a string
b = '010'; % Binary number b as a string
c = [b, a]; % Concatenate b and a
disp(c); % Display the result
This code will produce the desired output c = '0101100'.
Explanation:
  • String Representation: By treating the binary numbers as strings, you can easily concatenate them using the [] operator.
  • Concatenation: The [] operator is used to concatenate strings or arrays in MATLAB.
If you need c to be in a numeric binary format after concatenation, you can convert the string back to a number:
c_numeric = bin2dec(c); % Convert the binary string to a decimal number
This will give you the decimal equivalent of the binary number c. However, for binary operations and representation, keeping it as a string is often more convenient.

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by