You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MATLAB中矩阵位置坐标与标量相乘结果自动取整问题排查

解决MATLAB中位置矩阵乘小数自动取整的问题

嘿,这个问题我之前也碰到过!问题出在vision.LocalMaximaFinder输出的位置矩阵的数据类型上——它默认是整数类型(比如uint32或者int32),当你直接用整数乘以小数0.1时,MATLAB会自动把结果强制转回整数类型,导致小数部分被截断,看起来就像是自动取整了。

解决方案

解决方法很简单,只需要在乘法前把位置矩阵转换成浮点类型(比如double或者single),这样运算后就能保留小数了:

修正后的完整代码

hLocalMax = vision.LocalMaximaFinder; 
hLocalMax.MaximumNumLocalMaxima = 22500; 
hLocalMax.NeighborhoodSize = [3 3]; 
hLocalMax.Threshold = 0; 

location = step(hLocalMax, A); 
new_location = unique(location,'rows'); 
new_location = double(new_location).*0.1; % 先转换为浮点类型再执行乘法
disp(new_location)

额外说明

  • 你可以通过class(location)命令查看原始location变量的数据类型,大概率会看到是整数类(比如uint32)。
  • 如果不需要双精度浮点,用single(new_location)转换也能达到同样的效果,运算后会保留小数部分。

内容的提问来源于stack exchange,提问作者as94

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:33:11