请考虑以下示例:
Bathymetry = [0,4134066; 3,3817906; 6,3343666; 9,2978725; 12,2742092; 14,2584337; 16,2415355; 18,2228054; 20,2040753; 23,1761373; 26,1514085]; Depth = [0;1;2;3;5;8;10;11.6;15]; newDepth = min(Bathymetry(:,1)):0.1:max(Bathymetry(:,1));从这里我想找到'newDepth'的哪一列对应'Depth'。 例如:
dd = find(newDepth==Depth(1)) dd = 1显示Depth == 0,位于newDepth的第一列。 当我将它应用于'深度'的所有条目时
for i = 1:length(Depth); dd(i) = find(newDepth == Depth(i)); end我收到一个错误:
Improper assignment with rectangular empty matrix.最初我无法理解为什么,但是通过查看newDepth的数组,特别是第117行newDepth == 11.6,我注意到该值不等于11.6但等于11.600000000000001,因此与Depth(8)不同。 我怎样才能解决这个问题? 为什么MATLAB不只是将值写为11.6? 我没有指定包括.000000000000001。
Consider the following example:
Bathymetry = [0,4134066; 3,3817906; 6,3343666; 9,2978725; 12,2742092; 14,2584337; 16,2415355; 18,2228054; 20,2040753; 23,1761373; 26,1514085]; Depth = [0;1;2;3;5;8;10;11.6;15]; newDepth = min(Bathymetry(:,1)):0.1:max(Bathymetry(:,1));From this I want to find which column of 'newDepth' corresponds to 'Depth'. For example:
dd = find(newDepth==Depth(1)) dd = 1Showing that Depth == 0, is located in the first column of newDepth. When I apply this to all of the entries of 'Depth'
for i = 1:length(Depth); dd(i) = find(newDepth == Depth(i)); endI receive an error:
Improper assignment with rectangular empty matrix.Initially I couldn't understand why, but by looking at the array for newDepth, especially column 117 where newDepth == 11.6, I noticed that the value isnt equal to 11.6 but equal to 11.600000000000001 thus being different from Depth(8). How can I fix this? and why does MATLAB not just write the value as 11.6? nowhere have I specified to include the .000000000000001.
最满意答案
这是因为二进制中没有精确的0.1表示。 阅读维基以获取更多背景信息。 在二进制中,表示0.1就像试图写出三分之一的所有小数:
1/3 == 0.333333333333333333...无论你添加了多少3 ,它都永远不会准确。
出于这个(以及其他许多)原因,我建议你不要使用== (这是一个非常严格的要求),而是使用
for ii = 1:length(Depth); [~,dd(ii)] = min( abs(newDepth-Depth(ii)) ); endThis is because there is no exact representation of 0.1 in binary. Read the wiki for more background. In binary, representing 0.1 is something like trying to write out all the decimals of one-third:
1/3 == 0.333333333333333333...it will never be exact, no matter how many 3's you add.
For this (and many other) reasons, I'd suggest you do not use == (which is a very stringent demand), but rather use
for ii = 1:length(Depth); [~,dd(ii)] = min( abs(newDepth-Depth(ii)) ); end更多推荐
发布评论