Hi,
I have 2 numbers that I want to match in my program and if these don't match I want to receive an error message. The two numbers that I want to match should match only up to the first two decimal points. For example,
A=3.82 and B=3.829 should match since 3.82 is common. On the other hand, A=3.82 and B=3.8 don't match .How can I match A and B?

 채택된 답변

Chandra Kurniawan
Chandra Kurniawan 2012년 1월 8일

0 개 추천

A = 3.82;
B = 3.829;
C = 3.82;
D = 3.8;
Astr = num2str(A);
Bstr = num2str(B);
Astr = Astr(strfind(Astr,'.'):end);
Bstr = Bstr(strfind(Bstr,'.'):end);
if length(Astr) < 3, Astr = strcat(Astr,'0'); end
if length(Bstr ) < 3, Bstr = strcat(Bstr,'0'); end
strcmp(Astr(1:3),Bstr(1:3))

추가 답변 (1개)

Robert Cumming
Robert Cumming 2012년 1월 8일

3 개 추천

compare = @(n1,n2,n_dcp) round(n1*10^n_dcp)==round(n2*10^n_dcp);
>> compare(1.23,1.234,2)
ans =
1
>> compare(1.23,1.2,2)
ans =
0

댓글 수: 1

Anu
Anu 2024년 4월 16일
'compare' function requires one of the following Toolbox:
1. System Identification Toolbox
2. Model Predictive Control Toolbox
3. Predictive Maintenance Toolbox
Is there any alternative method

댓글을 달려면 로그인하십시오.

카테고리

태그

질문:

2012년 1월 8일

댓글:

Anu
2024년 4월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by