Convert decimal to binary in Matlab?

后端 未结 2 1153
情歌与酒
情歌与酒 2021-01-24 14:21

I am converting base-10 numbers to base-2 numbers, and specifying the number of bits I\'d like to use to represent these base-10 numbers.

Here\'s my code for negative nu

相关标签:
2条回答
  • 2021-01-24 15:18

    You need to reduce largest num when you put a zero in the output array, because you're essentially starting from a binary array of all ones (ie largestNum). This code worked for me:

    if decimal > 0
    largestNum = (2^(bits-1))-1;
    
    if decimal > largestNum
        error('%d cannot be represented in %d bits. Increase the number of bits. ',decimal,bits);
        output = '';
    end
    
    % first spot must be zero to show it\'s a positive number
    output = '0';
    bits = bits - 1;
    
    largestNum = largestNum + 1;
    num = largestNum;
    
    while num ~= decimal
        num = largestNum - 2^(bits-1);
        if num > decimal
            output = [output,'0'];
            largestNum = largestNum - 2^(bits-1);
        end
        if num <= decimal
            output = [output,'1'];
    
        end
        bits = bits - 1;
    end
    
    while bits ~= 0
        output = [output,'0'];
        bits = bits - 1;
    end
    end
    

    I'm not sure what this is for, but I would highly recommend using the built in dec2bin to do this.

    0 讨论(0)
  • 2021-01-24 15:18

    you can use this script in matlab:

    a=[1 2 3 4;-2 -4 3 4;7 8 9 4];
    
    [c,v]=size(a);
    
    n3=c*v;
    
    word_len=5;%with bits of binary  word
    
    data = reshape(a', n3, 1);
    
    databin= fi(data,1,5);
    
    h=bin(databin)%result
    
    0 讨论(0)
提交回复
热议问题