the number 1 that is most right of binary number

Discussion in 'MATLAB' started by Kate, Feb 17, 2011.

  1. Kate

    Kate Guest

    Hello newsgroup,

    In some of my work , I am dealing with binary numbers.

    Dec Binary
    ----- -------------------
    1 1 0 0 0
    2 0 1 0 0
    3 1 1 0 0
    4 0 0 1 0
    5 1 0 1 0
    6 0 1 1 0
    7 1 1 1 0
    8 0 0 0 1
    9 1 0 0 1
    10 0 1 0 1

    After converting a decimal number to its binary form using the "de2bi" function, for each digit, I need to find the position of number 1 that is most right for each binary number, counting from right.

    So in the above table with the decimal numbers and their binary conversions,for the 10 numbers, I'd like to know how can I output the following which gives the position of their "most right " number 1:

    Position of "Most Right" number 1 =
    3
    2
    2
    1
    1
    1
    1
    0
    0
    0

    If any of you guys know hoe can I approach this in Matlab, please send some assistance my way.

    thanks
    Kate
     
    Kate, Feb 17, 2011
    #1
    1. Advertisements

  2. Kate

    Paulo Silva Guest

    DecimalNumber=7; %lets test with the number 7
    find(de2bi(DecimalNumber),1,'last')
     
    Paulo Silva, Feb 17, 2011
    #2
    1. Advertisements

  3. Kate

    Paulo Silva Guest

    Here's a nice table

    d = (1:10)';
    b = de2bi(d)
    disp(' Dec Binary Pos')
    disp(' ----- ------------------- -----')
    for a=1:size(b,1)
    c(a)=find(de2bi(b(a,:)),1,'last');
    end
    disp([d, b, c'])
     
    Paulo Silva, Feb 17, 2011
    #3
  4. Kate

    Kate Guest

    thanks Paulo,

    But I am getting :

    1
    2
    2
    3
    3
    3
    3
    4
    4
    4

    How can I get the following as described in my first post:

    3
    2
    2
    1
    1
    1
    1
    0
    0
    0

    thanks again
    Kate
     
    Kate, Feb 17, 2011
    #4
  5. Kate

    Rune Allnor Guest

    What you want, then, is the position, counted from the right,
    of the right-most character in a string s that equals the
    character '1':

    s = '101100001000';
    n = length(s)+1-max(find(s=='1'))

    n =

    4

    Rune
     
    Rune Allnor, Feb 17, 2011
    #5
  6. Kate

    Rune Allnor Guest

    .... or if you want base-0 indexing:

    s = '101100001000';
    n = length(s)-max(find(s=='1'))

    n =

    3

    Rune
     
    Rune Allnor, Feb 17, 2011
    #6
  7. Kate

    Kate Guest

    thank you rune
     
    Kate, Feb 17, 2011
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.