Matlab 中数字的二进制表示

2023-12-10

有没有一个Matlab函数可以返回浮点数的二进制表示?


在Matlab中,可以使用Java JDK函数。

在 Matlab 中将 float(单精度 32 位数字)转换为二进制字符串表示形式的简短答案可能是:

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

长答案:Matlab 中的 float(单精度 32 位数字)转换为二进制字符串表示形式

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

将二进制字符串转换为浮点型,需要一点开销:

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Matlab 中数字的二进制表示 的相关文章

  • 为什么旋转 3D 点云后顶点法线会翻转?

    我有两个人脸 3D 点云样本 蓝色点云表示目标面 红色点云表示模板 下图显示目标面和模板面在不同方向上对齐 目标面大致沿 x 轴 模板面大致沿 y 轴 Figure 1 The region around the nose is displ
  • 如何在 Matlab 中对数组应用低通或高通滤波器?

    有没有一种简单的方法可以将低通或高通滤波器应用于 MATLAB 中的数组 我对 MATLAB 的强大功能 或数学的复杂性 有点不知所措 需要一个简单的函数或一些指导 因为我无法从文档或网络搜索中找到答案 看着那 这filter http w
  • 什么是 ANN 中的纪元以及它如何转换为 MATLAB 中的代码?

    我试图理解 并可视化 训练人工神经网络的时代到底是什么 我们有一个包含约 7000 个产品的训练集 其中有 10 个特征 输入 这些产品必须根据这 10 个输入分为 7 个类别 我们的 ANN 有 10 个输入 这些输入进入由 10 个神经
  • 如何在 C++ 中操作和表示二进制数

    我目前正在尝试使用非常简单的前序遍历算法为霍夫曼树构建一个查找表 但我在执行非常基本的按位操作时遇到了困难 伪代码如下 void preOrder huffNode node int bit not sure how to represen
  • 如何将二进制值列表转换为int32类型?

    我在 MATLAB 工作区中有一个小端格式的二进制数列表 我想将它们转换为 int32 a是由 0 和 1 组成的双向量 如下所示 a 0 0 0 1 1 0 0 1 1 1 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1 1
  • 在 Matlab 中将 datenum 转换为 datetime 的最快方法

    我在 Matlab 中将 datenum 转换为 datetime 时遇到问题 Given dnum floor now floor now 1 我尝试了以下方法 datenum dnum 但这没有用 我发现有效的方法是 datetime
  • 如何判断一个int数是奇数还是偶数? (二进制方式)

    我想利用基础知识来提高代码效率 我知道在二进制系统中 当数字的最后一位是 1 时 这是奇数 0 是偶数 在python中如何用这种方式判断一个int数 python 是否提供了任何内置方法来执行此操作 和 1 000010100100010
  • 在 Matlab 中高效获取像素坐标

    我想在 Matlab 中创建一个函数 给定一个图像 该函数将允许人们通过单击图像中的像素来选择该像素并返回该像素的坐标 理想情况下 人们能够连续单击图像中的多个像素 并且该函数会将所有相应的坐标存储在一个矩阵中 有没有办法在Matlab中做
  • Float 和 Int 都是 4 字节?怎么会?

    Int 为 4 个字节 范围为 2 31 浮点数为 4 个字节 范围为 1 2E 38 Float 包含实线上更多的点 但等于 int 的大小 float 的符号 指数 分数表示形式是否如此出色 或者 Int 的 2 的补码如此可悲 以至于
  • Kotlin 中 Float 和 Double 类型的最小值和最大值

    找出确切的内容很简单min and max值Int and LongKotlin 中的整数 有符号 32 位整数 Int MIN VALUE 2147483648 Int MAX VALUE 2147483647 有符号 64 位整数 Lo
  • 从字符串列表创建 TfRecords 并在解码后在张量流中提供图形

    目的是创建 TfRecords 数据库 给定 我有 23 个文件夹 每个文件夹包含 7500 个图像 以及 23 个文本文件 每个文件有 7500 行描述单独文件夹中 7500 个图像的特征 我通过以下代码创建了数据库 import ten
  • 如何找到平面和 3d 矩阵之间的交平面

    如果我有一堆图像并且尺寸如下 size M 256 256 124 我有 3 个点 它们的坐标是 coor a 100 100 124 coor b 256 156 0 coor c 156 256 0 如何创建 M 与这 3 个点定义的平
  • 将浮点数 1864.78 转换为二进制和 IEEE 格式

    我一直在尝试将 S P 500 的值 今天为 1864 78 转换为它在内存中以 IEEE 单精度格式表示的方式 转换小数点左边 1864 很容易 11101001000 但如何获得十进制 78 的二进制表示形式呢 我尝试使用该技术 但它会
  • 通过 Matlab 访问 Physionet 的 ptbdb 中的数据库

    我首先设置系统 old path which rdsamp if isempty old path rmpath old path 1 end 8 end wfdb url http physionet org physiotools ma
  • MATLAB - 冲浪图数据结构

    我用两种不同的方法进行了计算 对于这些计算 我改变了 2 个参数 x 和 y 最后 我计算了每种变体的两种方法之间的 误差 现在我想根据结果创建 3D 曲面图 x gt on x axis y gt on y axis Error gt o
  • 图像处理 - 使用 opencv 进行服装分割

    我正在使用 opencv 进行服装特征识别 第一步 我需要通过从图像中移除脸部和手来分割 T 恤 任何建议表示赞赏 我建议采用以下方法 Use 阿德里安 罗斯布鲁克的用于检测皮肤的皮肤检测算法 谢谢罗莎 格隆奇以获得他的评论 在方差图上使用
  • Matlab 的 imresize 函数中用于插值的算法是什么?

    我正在使用 Matlab Octaveimresize 对给定的二维数组重新采样的函数 我想了解如何使用特定的插值算法imresize works 我在Windows上使用八度 e g A 1 2 3 4 是一个二维数组 然后我使用命令 b
  • C 编程 - 文件 - fwrite

    我有一个关于编程和文件的问题 while current NULL if current gt Id Doctor 0 current current gt next id doc current gt Id Doctor if curre
  • @(t)在Matlab中是什么意思? [复制]

    这个问题在这里已经有答案了 正如标题所示 考虑到下面的上下文 t 在 Matlab 中到底意味着什么 computeNumericalGradient 是一个函数 cofiCostFunc 也是一个接受一堆参数的函数 问题是 t 对 cof
  • 哪种 C 数据类型可以表示 40 位二进制数?

    我需要表示一个40位的二进制数 应该使用哪种 C 数据类型来处理这个问题 如果您使用的是 C99 或 C11 兼容编译器 则使用int least64 t以获得最大的兼容性 或者 如果您想要无符号类型 uint least64 t 这些都定

随机推荐