フィルターのクリア

What is wrong with this code for finding out the number of significant digits after decimal?

2 ビュー (過去 30 日間)
Devadathan S
Devadathan S 2024 年 6 月 1 日
回答済み: Image Analyst 2024 年 6 月 1 日
I have created this code for finding the number of siignificant digits after decimal for Cody, but for some reason, it shows error (semantic). Can anyone please help identify why? Thank you.
The error comes when I apply:
x = [1.000 1.04 0.22 10.1; 2.05 2.33 4.1 1000.31; 5.00010 6.429 7.492 8.0]
I tried debugging, but the error comes due to 6.429 having a '1' after another 11 zeroes.
Code:
function y = find_max_sigdec(x)
stop = 0;
i = 0;
if x == fix(x)
y = 0;
else
while stop == 0
x = x*10;
i = i+1;
if x == fix(x)
y = i;
stop = 1;
end
end
end
end
  3 件のコメント
Devadathan S
Devadathan S 2024 年 6 月 1 日
Wow. Thanks. I remember something similar in python but ever came acrosss something like this MATLAB despite doing way complex stuff for a while. So, if I have to do this problem, should I change the logic altogether or should I assign a tolerence? Which is better?
Fangjun Jiang
Fangjun Jiang 2024 年 6 月 1 日
編集済み: Fangjun Jiang 2024 年 6 月 1 日
You need to change the type of your input then process it.
x = ["1.000" "1.04" "0.22"]
x = 1x3 string array
"1.000" "1.04" "0.22"
y=6.429
y = 6.4290
format long
y
y =
6.429000000000000

サインインしてコメントする。

回答 (2 件)

Stephen23
Stephen23 2024 年 6 月 1 日
編集済み: Stephen23 2024 年 6 月 1 日
The least unexpected number of non-zero fractional digits aka decimal places (limited to 15 significant figures of the binary floating point numbers):
format long G
x = [1.000, 1.04, 0.22, 10.1; 2.05, 2.33, 4.1, 1000.31; 5.00010, 6.429, 7.492, 8.0]
x = 3x4
1.0e+00 * 1 1.04 0.22 10.1 2.05 2.33 4.1 1000.31 5.0001 6.429 7.492 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
str = compose("%.15e",x(:));
tkn = regexp(str,'^\d+\.(\d*[1-9])?0*[eE]((+|-)\d+)$','tokens','once');
tkn = vertcat(tkn{:});
dcp = strlength(tkn(:,1))-str2double(tkn(:,2));
dcp = max(0,reshape(dcp,size(x)))
dcp = 3x4
0 2 2 1 2 2 1 2 4 3 3 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Image Analyst
Image Analyst 2024 年 6 月 1 日
See the FAQ
One way to compare floating point numbers is to use ismembertol
help ismembertol
ISMEMBERTOL Set ismember within a tolerance ISMEMBERTOL is similar to ismember. Whereas ismember performs exact comparisons, ISMEMBERTOL performs comparisons using a tolerance. LIA = ISMEMBERTOL(A,B,TOL) returns an array of the same size as A containing logical 1 (true) where the elements of A are within the tolerance TOL of the elements in B; otherwise, it contains logical 0 (false). ISMEMBERTOL scales the TOL input based on the magnitude of the data, so that two values u and v are within tolerance if: abs(u-v) <= TOL*max(abs([A(:);B(:)])) LIA = ISMEMBERTOL(A,B) uses a default tolerance of 1e-6 for single precision inputs and 1e-12 for double precision inputs. [LIA,LOCB] = ISMEMBERTOL(A,B) also returns an array, LOCB, which contains an index location in B for each element in A which is a member of B. [...] = ISMEMBERTOL(...,'Name1',Value1,'Name2',Value2,...) specifies one or more of the following Name/Value pair arguments using any of the previous syntaxes: OutputAllIndices - When true, this returns a cell-array LOCAllB (instead of LOCB) as the second output, indicating all the indices in B that are within tolerance of a value in A. The default value is false. ByRows - When true, this returns an array LIA indicating whether each row of A is within tolerance of a row in B. Two rows u and v are within tolerance if: all(abs(u-v) <= TOL*max(abs([A;B]))) Use this to find rows that are within tolerance of each other. Each column is considered separately. The default value is false. DataScale - A scalar which changes the tolerance test to be: abs(u-v) <= TOL*DS Specify a value DS if you want to specify an absolute tolerance. When used together with the ByRows option, the DataScale value may also be a vector. In that case, each element of the vector specifies DS for a corresponding column in A. If a value in the DataScale vector is Inf, then ISMEMBERTOL ignores the corresponding column in A. Example: % A is a matrix of real values A = [0.05, 0.11, 0.18; 0.18, 0.21, 0.29; 0.34, 0.36, 0.41; 0.46, 0.52, 0.76; 0.82, 0.91, 1.00]; % The entries of B are the same as the entries of A within round-off error. B = log10(10.^A); % A and B differ by small amounts A-B % ismember uses exact equality, most of the rows in A do not get matched % with rows in B ismember(A,B,'rows') % By default ISMEMBERTOL uses a small tolerance and the rows will be matched ismembertol(A,B,'ByRows',true) See also: ISMEMBER, UNIQUETOL Documentation for ismembertol doc ismembertol Other uses of ismembertol gpuArray/ismembertol

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by