How to keep leading zeros by transforming array to single numbers

Raldenors

So my problem was, when i'm trying to combine number from different columns, leading zeros disappear, here's my code:

Bthree= [0  0   1
9   1   2
0   5   7]


len=size(Bthree);
    A=[];
    for jj=1:len(1)
        s=int2str(Bthree(jj,1:3));
        s=s(s~=' ');
        A(jj,:)=[str2num(s)];
    end

Output

1
912
57

as you can see leading zeros disappear but i want zero to be keep

desired output:

001
912
057

so can i do that? thanks

thewaywewalk

If you want to keep leading zeros you likely need to store your data as strings:

Bcell = strrep(cellstr(num2str(Bthree)),' ','')

returns a cell array of strings our your numbers. For a char array additional do:

Bchar = cell2mat(Bcell)

Or alternatively you can get the char array directly by:

Bchar = reshape(sprintf('%i',Bthree),size(Bthree))

returning:

Bcell = 

    '001'
    '912'
    '057'


Bchar =

001
912
057

As you seem to be not sure if you really need the leading zeros, here a short way for the conversion to doubles:

Bdouble = str2num(Bchar)

Bdouble =

     1
   912
    57

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

how to store zero leading numbers in mongoDB

분류에서Dev

Transforming letters in columns into numbers

분류에서Dev

Transforming letters in columns into numbers

분류에서Dev

Transforming XML content to array

분류에서Dev

Trying to join two tables on a zip - one table has no leading zeros, the other has leading zeros

분류에서Dev

BTRFS, Files on a single drive in array but keep 2-parity?

분류에서Dev

For for Numpy array with zeros and ones, how to remove rows with duplicate ones?

분류에서Dev

Leading zeros in IPv4 address; is that a no-no by convention or standard?

분류에서Dev

How to keep pandas loc from transposing a single item dataframe

분류에서Dev

How to to keep this merge array method in-bounds?

분류에서Dev

When normalizing list of numbers, result is all zeros

분류에서Dev

C How to convert ASCII numbers in an array to text

분류에서Dev

How to get average of first two numbers in array?

분류에서Dev

Dealing with zeros in numpy array normalization

분류에서Dev

How to display json array values in single UITableVIewCell

분류에서Dev

How to reference a subarray of a two dimensional array as a single dimensional array in java

분류에서Dev

How to keep data stored in array after application has ended?

분류에서Dev

How to separate volume level for front and rear panels and yet keep single sound device?

분류에서Dev

How do I keep single quotes intact when returning the output of a Bash subshell to the OSX "defaults" utility?

분류에서Dev

Remove non-word characters and leading numbers from string

분류에서Dev

shifting elements in an array of strings and fill with zeros

분류에서Dev

Octave - Creating a sequence of odd numbers between 1 and 10 separated by zeros

분류에서Dev

transform an array of array to an array of numbers

분류에서Dev

Count number of numbers in a single cell

분류에서Dev

How do I add only the positive numbers in a ruby array?

분류에서Dev

How can I encode and decode an Array of Numbers in Objective-C?

분류에서Dev

Using an array, how do I store and display the unique numbers?

분류에서Dev

AS3 - How to find TWO biggest numbers in Array

분류에서Dev

How to add leading spaces to output column

Related 관련 기사

  1. 1

    how to store zero leading numbers in mongoDB

  2. 2

    Transforming letters in columns into numbers

  3. 3

    Transforming letters in columns into numbers

  4. 4

    Transforming XML content to array

  5. 5

    Trying to join two tables on a zip - one table has no leading zeros, the other has leading zeros

  6. 6

    BTRFS, Files on a single drive in array but keep 2-parity?

  7. 7

    For for Numpy array with zeros and ones, how to remove rows with duplicate ones?

  8. 8

    Leading zeros in IPv4 address; is that a no-no by convention or standard?

  9. 9

    How to keep pandas loc from transposing a single item dataframe

  10. 10

    How to to keep this merge array method in-bounds?

  11. 11

    When normalizing list of numbers, result is all zeros

  12. 12

    C How to convert ASCII numbers in an array to text

  13. 13

    How to get average of first two numbers in array?

  14. 14

    Dealing with zeros in numpy array normalization

  15. 15

    How to display json array values in single UITableVIewCell

  16. 16

    How to reference a subarray of a two dimensional array as a single dimensional array in java

  17. 17

    How to keep data stored in array after application has ended?

  18. 18

    How to separate volume level for front and rear panels and yet keep single sound device?

  19. 19

    How do I keep single quotes intact when returning the output of a Bash subshell to the OSX "defaults" utility?

  20. 20

    Remove non-word characters and leading numbers from string

  21. 21

    shifting elements in an array of strings and fill with zeros

  22. 22

    Octave - Creating a sequence of odd numbers between 1 and 10 separated by zeros

  23. 23

    transform an array of array to an array of numbers

  24. 24

    Count number of numbers in a single cell

  25. 25

    How do I add only the positive numbers in a ruby array?

  26. 26

    How can I encode and decode an Array of Numbers in Objective-C?

  27. 27

    Using an array, how do I store and display the unique numbers?

  28. 28

    AS3 - How to find TWO biggest numbers in Array

  29. 29

    How to add leading spaces to output column

뜨겁다태그

보관