How to convert a matrix having integer entries (+ or -) into binary ?

조회 수: 6 (최근 30일)
Abdul Gaffar
Abdul Gaffar 2018년 4월 27일
댓글: Abdul Gaffar 2018년 4월 28일
i have code,
a = -123; % your float point number
n = 16; % number bits for integer part of your number
m = 0; % number bits for fraction part of your number
% binary number
d2b = fix(rem(a*pow2(-(n-1):m),2));
% the inverse transformation
b2d = d2b*pow2(n-1:-1:-m).';
sir, this code converts floating point no. (+ or -) to binary. If i have a matrix say A=[1,2,3;-1,-2,5;-5,6,-23], then how to do the same ??

답변 (1개)

Ameer Hamza
Ameer Hamza 2018년 4월 28일
This code will work for a matrix A
% for transformation
powers = reshape(pow2(-(n-1):m), 1, 1, []);
d2b = fix(rem(a.*powers, 2));
% for inverse transformation
powerReverse = reshape(pow2(n-1:-1:-m), 1, 1, []);
b2d = sum(d2b.*powerReverse, 3);
d2b is 3D matrix. To extract transformation value corresponding to a(i, j) from d2b use squeeze(d2b(i,j,:)).
  댓글 수: 5
Ameer Hamza
Ameer Hamza 2018년 4월 28일
I question you just mentioned a 2D matrix A. It seems that you are reading an RGB image. A color image have 3 dimensions. You first need to use rgb2gray() to convert it to a 2D grayscale image. Try this
a=imread('lena.png');
a = rgb2gray(a);
a = double(a);
Abdul Gaffar
Abdul Gaffar 2018년 4월 28일
Error using .* Integers can only be combined with integers of the same class, or scalar doubles. Error in dec2bin_mat (line 4) d2b = fix(rem(a.*powers, 2));

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by