-1

Write a function encodedgt that takes as input a column or row vector with integer entries and produces as output a row vector where each integer is encoded according to the following table

Integer Encoding 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 2 0 0 1 0 0 1 1 3 0 1 1 1 1 0 1 ... etc.

and the rule encodedgt(x) = 1 - encodedgt(-(x+1)) when x < 0.

For example,

encodedgt([1 12 -3])

[0 0 1 1 0 0 1 0 1 0 1 0 1 1 0 1 1 0 0]


So I already wrote a script that works fine with positive numbers which is the following...

function x = encodedgt(x)

enc = {0, '0 0 0 1 1 0 1'; 1 , '0 0 1 1 0 0 1'; 2, '0 0 1 0 0 1 1';... 3, '0 1 1 1 1 0 1'; 4, '0 1 0 0 0 1 1'; 5, '0 1 1 0 0 0 1'; 6, ... '0 1 0 1 1 1 1'; 7, '0 1 1 1 0 1 1'; 7, '0 1 1 1 0 1 1'; 8, ... '0 1 1 0 1 1 1'; 9, '0 0 0 1 0 1 1'; 10, '0 0 0 0 1 0 1'; 11, ... '1 0 1 0 0 0 0'; 12, '0 1 0 1 0'};

y = str2num(cell2mat(arrayfun(@(v)enc{find([enc{:, 1}]) == v, 2}', x(:), 'UniformOutput',0)))'

end

HOWEVER, I'm not entirely sure how to get it to work with negative numbers. As the question tells me, encodedgt(x) = 1 - encodedgt(-(x+1)) when x < 0. So I thought about using an if statement but er it's not coming out exactly to plan. Is there a better way to approach this?

Tofurkey
  • 1
  • 3

1 Answers1

0

Your question isn't very specific so it is hard to know exactly what you need. Some tools you will need:

  • how to concatenate vectors. For this, use cat or just the following approach:

    sequence=[0 0 1 1 0 0 1];
    sequence=[sequence [0 0 1 0 0 1 1]]

    sequence = 0 0 1 1 0 0 1 0 0 1 0 0 1 1

  • how to iterate through a vector. For this, use for:

    function(x)
    for i=x
    % do what you need, like a lookup of i to get it's encoded vector end

Lolo
  • 3,935
  • 5
  • 40
  • 50