converting decimal matrix to binary matrix

I wish to convert a matrix of decimals to a matrix of binary numbers.
For example, if M = [ 1 25 34 9 ]
I then want, M = [ 00000001 00011001 00100010 00001001]
I got the output for the following matrix M=[6 5;1 3] from the below code.,but it doesn't work on the above matrix
% M=[6 5;1 3];
% b=dec2bin(M');
% b=b';
% b=str2num(b(:));
% b=reshape(b,8,2)'
*Any help/pointers would be appreciated.*

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 3 月 27 日

1 投票

M = [ 1 25 34 9 ]
out=cellstr(dec2bin(M,8))'

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2014 年 3 月 27 日

0 投票

Do you want the output in strings, as cells, or as numbers?
M = [ 1 25 34 9 ]
Mstr = dec2bin(M,8) % char array
Mcell = cellstr(dec2bin(M,8)).' % cell array
Mnum = dec2bin(M,8)-'0' % numerical array

4 件のコメント

Anu Sharma
Anu Sharma 2014 年 3 月 28 日
thanks a lot :) it worked !!
Anu Sharma
Anu Sharma 2014 年 3 月 28 日
In the same way i applied your code for the image i.e.,reading the values of image and converting it to binary matrix.
I=imread('lena.jpg');
J=dec2bin(I(:),8)-'0'; % numerical array
but when i tried to access J it shows " Cannot display variables with more than 524288 elements."
Is there any solution
thanks in advanceee
Jos (10584)
Jos (10584) 2014 年 3 月 28 日
Show parts of it
p = 1:100
disp(J(p,:))
But are you sure this is the most efficient way to look at your data?
sam dhaka
sam dhaka 2016 年 2 月 1 日
how can i make binary matrix from decimal matrix, suppose i have A=[2 3 4, 5 6 7, 1 2 1, 7 8 9] matrix and i want this like B=[1 1 1, 1 1 1, 0 0 0, 1 1 1]

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

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by