Hi guys.could you help me please?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Steven Lord
il 9 Gen 2018
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.
Risposte (2)
Birdman
il 9 Gen 2018
Modificato: Birdman
il 9 Gen 2018
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 Commenti
Birdman
il 9 Gen 2018
Modificato: Birdman
il 9 Gen 2018
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
il 9 Gen 2018
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 Commenti
Jan
il 9 Gen 2018
@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.
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!