Search code examples
matlabgalois-field

Error in gfconv (Galois field function) in Matlab


I am trying to multiply a hexdecimal value AA by 2 in the galois field using the gfconv(a,b) function of Matlab, the console returns to me an error saying: "The input elements must be binary.", but my two elements are binary

a=hexToBinaryVector('AA');
b=de2bi(2);
c=gfconv(a,b);
disp(c);

The error code:

Error using gfconv_mex
The input elements must be binary.

Error in gfconv (line 120)
        c = gfconv_mex(varargin{:});

Error in test(line 3)
c=gfconv(a,b);

Any idea how to solve that?


Solution

  • I'm not doing all the code here, but here's the steps that I would take to solve it.

    Problem

    It's in binary form, but b is a vector of binary numbers.

    Solution

    1. Look up https://www.mathworks.com/help/matlab/ref/mat2str.html
    2. Convert your vector to the string
    3. Parse the string and pull out the ones and zeros
    4. use str2num to convert back to a number: https://www.mathworks.com/help/matlab/ref/str2num.html

    Try that and see if it works.

    Note - Expansion of step 3:

    if we have variable a = '[0+11]';, we can select individual characters from the string like:

     a(3)
    ans = '+'
    a(4)
    ans = '1'
    a(1)
    and = '['
    

    Therefore, you can parse the string with a loop"

    for n = 1 : length(a)
        if a(n) == '1' || a(n) == '0'
            str(n) = a(n);
        end
    end
    

    Finally, convert the string:

    num = str2num(str);
    

    done