Hi guys.could you help me please?

1 回表示 (過去 30 日間)
Hussein Ali
Hussein Ali 2018 年 1 月 9 日
コメント済み: Walter Roberson 2018 年 1 月 9 日
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 件のコメント
Birdman
Birdman 2018 年 1 月 9 日
Check my answer.
Steven Lord
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
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
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.
Walter Roberson
Walter Roberson 2018 年 1 月 9 日
Note that sprintf() is not supported by MATLAB Coder.

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


Jan
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 件のコメント
Stephen23
Stephen23 2018 年 1 月 9 日
+1 nice concept.
Jan
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 ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by