如何修复和删除数字中的零以均衡数组中的数字
how to fix and remove zero among a number to equalize the digits in an array
考虑 MATLAB 中的数组:
a = [102 20 1 30 8 255];
在这个数组中,我需要通过在所有值前面加上零来使所有数字成为三位数:
a = 102 020 001 030 008 255
在那之后,我需要再次反转它。我怎样才能做到这一点?
我试图分开数字并执行此操作。但是失败了。
您可能需要转换为字符串。例如,查看 int2str
或 num2str
函数。然后您可以轻松地在开头连接零。例如:
s = int2str(10);
['0' s]
这会为您提供 010
作为输出。
然后您可以使用 str2num
函数恢复。
您想使用 notation of fprintf
, which can be saved as a string with sprintf
:
>> a = [102 20 1 30 8 255]
a =
102 20 1 30 8 255
>> b = sprintf('%.3d ',a) % b is a single string
b =
102 020 001 030 008 255
>> a = str2num(b)
a =
102 20 1 30 8 255
考虑 MATLAB 中的数组:
a = [102 20 1 30 8 255];
在这个数组中,我需要通过在所有值前面加上零来使所有数字成为三位数:
a = 102 020 001 030 008 255
在那之后,我需要再次反转它。我怎样才能做到这一点? 我试图分开数字并执行此操作。但是失败了。
您可能需要转换为字符串。例如,查看 int2str
或 num2str
函数。然后您可以轻松地在开头连接零。例如:
s = int2str(10);
['0' s]
这会为您提供 010
作为输出。
然后您可以使用 str2num
函数恢复。
您想使用 notation of fprintf
, which can be saved as a string with sprintf
:
>> a = [102 20 1 30 8 255]
a =
102 20 1 30 8 255
>> b = sprintf('%.3d ',a) % b is a single string
b =
102 020 001 030 008 255
>> a = str2num(b)
a =
102 20 1 30 8 255