Search code examples
matlabnumbersappenddigits

append digits to a number - Matlab


I have a column with only whole numbers in it, to which I need to append a number (say 11) at the front -

data = (1:1300000)';
% append 11 to these numbers

newdata = [111 ;  112 ; 113 ; 114 ; ......]

Is there a way to do it without using str2num (due to speed issues) ? Thanks.


Solution

  • If you take the base 10 logarithm of data, you can find out by how much you have to multiply 11 so that you can turn this into a simple addition.

    %# create some test data
    data = [1 22 123];
    
    %# add 11*10^x to data so that the two ones end up in front of the number
    newData = 11*10.^(floor(log10(data))+1)+data
    
    newData =
             111        1122       11123