2011-12-20 3 views

답변

4

Matlab에서 Java JDK 기능을 사용할 수 있습니다.

수도 이진 문자열 표현 매트랩 플로트 (단 정밀도 32 비트 수)를 변환하기위한 짧은 대답 :

flt=3.14 
import java.lang.Integer java.lang.Float; 
Integer.toBinaryString(Float.floatToIntBits(flt)) 

긴 않음 : 부동 소수점 변환 (단 정밀도 32 비트 수)

function out=binstring2float(binstr) 
    % converts a binary string to float number according to IEEE754 
    % 
    % Usage: 
    % binstring2float('11000000010010001111010111000011') 
    % -3.14 
    % 
    % 
    % http://www.h-schmidt.net/FloatApplet/IEEE754.html 
    % 
    % Limitations: 
    % Rounding errors: Not every decimal number can be expressed exactly as a floating 
    % point number. This can be seen when entering "0.1" and examining its binary representation which is either slightly smaller or larger, depending on the last bit. 
    % 
    % Andrej Mosat, [email protected] 
    % 03/2012 
    % v0.0 
    % License: GNU GPL 
    % 
    % See also: FLOAT2BINSTRING 

    import java.lang.Long java.lang.Float; 

    if isequal(class(binstr), 'java.lang.String') 
      binstr=char(binstr); 
    end 

    if (~isstr(binstr)) 
     error('input must be a binary string');            
    end 
    % Error handling for binary strings should be added here 

    % the sign is negative 

    if binstr(2)=='1' 
     binstr(2)=''; 
     isnegative=1; 
    else 
     isnegative=0; 
    end 

    out=Float.intBitsToFloat(Long.parseLong( binstr , 2)); 
    if isnegative 
    out=-out; 
    end 

end 
,536,913 : matlab에있는 바이너리 문자열 표현

function out=float2binstring(flt) 
% converts a float number to binary in matlab according to IEEE754 
% 
% Usage: 
% float2binstring(-3.14) 
% 
% http://www.h-schmidt.net/FloatApplet/IEEE754.html 
% 
% Limitations: 
% Rounding errors: Not every decimal number can be expressed exactly as a  floating 
% point number. This can be seen when entering "0.1" and examining its binary  representation which is either slightly smaller or larger, depending on the last bit. 
% 
% Andrej Mosat, [email protected] 
% 03/2012 
% v0.0 
% License: GNU GPL 
% 
% See also: BINSTRING2FLOAT 


% this is a trick to use java JDK should be installed, tested on Matlab R2010 
import java.lang.Integer java.lang.Float; 

if (~isnumeric(flt)) 
    error('input must be a number'); 
end 

out=Integer.toBinaryString(Float.floatToIntBits(flt)); 
end 

그리고 약간의 오버 헤드를 떠 이진 문자열의 변환

+0

네이티브 MATLAB 코드는 어떻습니까? – Royi

2

것 같습니다.

+0

또한 'hex'를 사용하여 표시 방법을 변경할 수 있습니다. –

관련 문제