フィルターのクリア

How can I convert a matrix into a number?

25 ビュー (過去 30 日間)
Jaybee
Jaybee 2014 年 3 月 12 日
コメント済み: Sagar Damle 2014 年 3 月 12 日
suppose I have a matrix:
matrix=[1 1 0 1 0 1 1]
then I want it to be
1101011.
I used mat2str, num2str but it didn't satisfied me.
Thankyou! More power to you guys!
  1 件のコメント
per isakson
per isakson 2014 年 3 月 12 日
Try the function sprintf

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

採用された回答

Mischa Kim
Mischa Kim 2014 年 3 月 12 日
編集済み: Mischa Kim 2014 年 3 月 12 日
Use
matstr = strrep(num2str(matrix),' ','')
bin2dec(matstr)
ans =
107
  2 件のコメント
Jaybee
Jaybee 2014 年 3 月 12 日
this helps! thankyou! ive learned a new code :)
Sagar Damle
Sagar Damle 2014 年 3 月 12 日
If your problem is to get 107 from [1 1 0 1 0 1 1],then you can read this :- There are many functions in MATLAB to convert base 10 to base 2 of a number –
1. dec2base()
2. dec2bin()
3. de2bi()
4. dec2binvec()
However,outputs of these are are not in same format. Run this code on your computer for understaning purpose. See help about these functions in MATLAB.
Note : '0' represents ASCII value of zero(0) which is 48. So,
ans2 = dec2base(4,2) - '0'
is same as
ans2 = dec2base(4,2) – 48
Play with these functions! ENJOY!
% Program -
temp1 = dec2base(4,2)
ans1 = str2num(dec2base(4,2))
ans2 = dec2base(4,2) - '0'
temp2 = dec2bin(4)
ans3 = dec2bin(4) - '0'
temp3 = de2bi(4)
temp4 = de2bi(4,'right-msb')
ans4 = de2bi(4,'left-msb')
temp5 = dec2binvec(4)
ans5 = seqreverse(dec2binvec(4))
whos

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

その他の回答 (1 件)

per isakson
per isakson 2014 年 3 月 12 日
Try
>> sprintf( '%1d', matrix )
ans =
1101011

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by