How to extract digits
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
Hello everybody. I would like to know how to extract the first digit of each element of a vector.
Example
a = [438;259;21;4;79876]
out = [4;2;2;4;7]
At the same time I would like to know if there is a way to extract the first two digits of each element:
a = [438;259;21;44;79876]
out = [43;25;21;44;79]
tnxxx
採用された回答
Voss
2022 年 3 月 23 日
Assuming they're all positive numbers, you can try this (the 2-digit result won't be accurate for one-digit numbers):
a = [438;259;21;44;79876];
power10 = 10.^floor(log10(a));
first_digit = floor(a./power10)
first_digit = 5×1
4
2
2
4
7
first_two_digits = floor(a./power10*10)
first_two_digits = 5×1
43
25
21
44
79
6 件のコメント
LC
2022 年 3 月 24 日
編集済み: Image Analyst
2022 年 3 月 24 日
Hi, very thanks for your help! I also wanted to ask you another question: if instead of a vector with numbers I had some text strings, how could I extract the first element?
Ex
a=['ab';'cde','hhh','kbj']
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Dimensions of arrays being concatenated are not consistent.
out=['a';'c';'h';'k']
tnxxxx
Image Analyst
2022 年 3 月 24 日
LC, that syntax is not right. What do you really have? A vector of strings (newer and less common)? Or a cell array of character arrays (older and more common)? They are different.
LC
2022 年 3 月 25 日
Hi, sorry if I've been unclear, I'm just starting out with matlab. I believe it is an array of characters. Now I'm sending you a photo of my variable so maybe it's clearer. Thanks.

Voss
2022 年 3 月 25 日
That is indeed an array of characters (in particular, it is a character vector - a vector being a 1-dimensional array; in this case it is a row vector, i.e., of size 1-by-something).
Here's how you could extract the first digit (or first two digits) of each number in there, resulting in a cell array of character vectors or a character array.
(The idea is to read the character vector ST using sscanf() to get numbers and then use the approach in my answer to get the numeric digit(s), then 'print' those digit(s) to into a cell array or character array using sprintfc() or sprintf().)
ST = '51 51 50 49 49';
a = abs(sscanf(ST,'%f'))
a = 5×1
51
51
50
49
49
power10 = 10.^floor(log10(a));
first_digit = sprintfc('%d',floor(a./power10))
first_digit = 5×1 cell array
{'5'}
{'5'}
{'5'}
{'4'}
{'4'}
first_two_digits = sprintfc('%d',floor(a./power10*10))
first_two_digits = 5×1 cell array
{'51'}
{'51'}
{'50'}
{'49'}
{'49'}
Those are the cell array results, and here's the character array version (with one or two columns depending on if you're taking one or two digits):
first_digit = sprintf('%d',floor(a./power10)).'
first_digit = 5×1 char array
'5'
'5'
'5'
'4'
'4'
first_two_digits = reshape(sprintf('%d',floor(a./power10*10)),2,[]).'
first_two_digits = 5×2 char array
'51'
'51'
'50'
'49'
'49'
Now, let's say ST was like this instead:
ST = '-5.1 +5.1e+002 50.0 -49e-3 49';
Then the approach above will still work:
a = abs(sscanf(ST,'%f'))
a = 5×1
5.1000
510.0000
50.0000
0.0490
49.0000
power10 = 10.^floor(log10(a));
first_digit = sprintfc('%d',floor(a./power10))
first_digit = 5×1 cell array
{'5'}
{'5'}
{'5'}
{'4'}
{'4'}
first_two_digits = sprintfc('%d',floor(a./power10*10))
first_two_digits = 5×1 cell array
{'51'}
{'51'}
{'50'}
{'49'}
{'49'}
first_digit = sprintf('%d',floor(a./power10)).'
first_digit = 5×1 char array
'5'
'5'
'5'
'4'
'4'
first_two_digits = reshape(sprintf('%d',floor(a./power10*10)),2,[]).'
first_two_digits = 5×2 char array
'51'
'51'
'50'
'49'
'49'
But let me reiterate that this approach still has the limitation that the two-digit result for a one-digit number will not be accurate ('4' will return '40' for instance).
In general, the best choice of method will depend on:
- what format(s) the numbers in the ST character vector might possibly be in, and
- what class of variable you want to end up with, e.g., cell array of character vectors, character array, string array, numeric array
Stephen23
2022 年 3 月 25 日
ST = '51 51 50 49 49';
DD = regexp(ST,'\d\d','match')
DD = 1×5 cell array
{'51'} {'51'} {'50'} {'49'} {'49'}
LC
2022 年 3 月 26 日
thank you very much for your support, it works great because I never have a one-digit number.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
