How to extract digits
31 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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_two_digits = floor(a./power10*10)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
