我正在尝试计算整数的长度.
例如:
a = 1.1234; b = 33; c = 100; d = -222;
e = lengthint([a,b,c,d])
Run Code Online (Sandbox Code Playgroud)
预期产量:
e = 1 2 3 3
Run Code Online (Sandbox Code Playgroud)
我试过用这个:
e = max(ceil(log10(abs([a,b,c,d]))),1)
Run Code Online (Sandbox Code Playgroud)
但这是输出:
e = 1 2 2 3
Run Code Online (Sandbox Code Playgroud)
所以数字的问题是10的倍数.
你可以这样做 -
A = [a,b,c,d]
lens = floor(log10(abs(A)))+1
lens(lens<0) = 0 %// Assuming that 0.xx numbers to have zero lengths
Run Code Online (Sandbox Code Playgroud)
样品运行:
情况1:
>> A = [0.00001234, 1.1234, 33, 10, -222];
>> lens = floor(log10(abs(A)))+1;
>> lens(lens<0) = 0
lens =
0 1 2 2 3
Run Code Online (Sandbox Code Playgroud)
案例#2:
>> A = [-0.00001234, 1.1234 33, 10, -222, 0];
>> lens = floor(log10(abs(A)))+1;
>> lens(lens<0) = 0
lens =
0 1 2 2 3 0
Run Code Online (Sandbox Code Playgroud)