Search code examples
matlabzerodigits

how to fix and remove zero among a number to equalize the digits in an array


consider an array in MATLAB :

a = [102 20 1 30 8 255];

In this array, I need to make all the numbers to three digits by prefixing zero to all values lie this :

a = 102 020 001 030 008 255

After that, I need to reverse it again to the same. how can i do this? I tried to separate the digits and do this. But it failed.


Solution

  • You want to use the 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