Separating digits in decimal representation of integer

后端 未结 3 350
我在风中等你
我在风中等你 2021-01-17 05:10

I want to take user input entered as an integer, for example (45697), and store the first two digits in an array, vector, or something else such as ( 4 5

3条回答
  •  一向
    一向 (楼主)
    2021-01-17 05:40

    You can do it easily using conversions to/from strings:

    >> x = 45697; % or whatever positive value
    >> str = num2str(x);
    >> y = [str2num(str(1)) str2num(str(2))]
    
    y =
    
         4     5
    

    This assumes that the number x is positive (if it were negative the first character would not be a digit). That seems to be the case, since according to your comments it represents an electrical resistance.

提交回复
热议问题