Hi guys.could you help me please?
1 次查看(过去 30 天)
显示 更早的评论
actually, i have three number, I want to compare the first digit of each number to the other
for example A=803.555 B=803.654 C=522.533 I have to compare 8,8 and 5 ..here they are not equal, therefore I have to do some things on the other hand A=803.555 B=803.654 C=803.544 I have to compare 8,8 and 5..here they are equal. so I have to do some things else
in general, how to write a commend to compare them thank you
4 个评论
Steven Lord
2018-1-9
Assuming you're able to extract that information, what are you planning to do with it? There may be a way to achieve your goal without trying to decompose the numbers into their individual digits.
回答(2 个)
Birdman
2018-1-9
编辑:Birdman
2018-1-9
Firstly, convert them to string.
A=num2str(A);
B=num2str(B);
C=num2str(C);
Then, compare their first digit by writing
str2double(A(1))>str2double(B(1))
or
A(1)>B(1)
Same applies for all combinations. Note that it is not relevant to convert them to numeric again.
9 个评论
Birdman
2018-1-9
编辑:Birdman
2018-1-9
The function 'num2str' is not supported for standalone code generation. See the documentation for coder.extrinsic to learn how you can use this function in simulation
Code generation is completely a different stuff. Simulink only works with numeric variables, therefore my approach will fail there. You need to consider another way.
[EDITED]:
Here is an implementation of Jan's function in Simulink: Check the attached model, run it and see the results.
Jan
2018-1-9
The conversion to a char vector by sprintf or num2str is short an easy sometimes, but internally a lot of computations are required for these functions. It might be more efficient to stay at the original data type and get the value of the first digit by using log10:
A = 803.555
B = 803.654
C = 522.533
A1 = getFirstDigit(A);
B1 = getFirstDigit(B);
C1 = getFirstDigit(C);
if isequal(A1, B1, C1)
...
else
...
end
function D = getFirstDigit(X)
D = floor(X ./ 10 .^ floor(log10(X)));
end
You can call this with an array also:
Digits = getFirstDigit([A, B, C])
2 个评论
Jan
2018-1-9
@Stephen: Thanks. The analysis of the input using log10 must happen internally in sprintf also, and in consequence in num2str. But getFirstDigit avoids to parse the rest of the number in addition.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!