2

I am trying to figure out how to encode integers based on given parameters. For example, I am given the table:

integer = 0, encoding is 3294 

integer = 1, encoding is 128 

integer = 2, encoding is 2098

etc.

How would I go about doing this? I have looked at the function dec2bin, but it's not exactly what I need. My input would be a vector, say x = [ 2 1 0 ] and the output would be the vector y = [2 0 9 8 1 2 8 3 2 9 4].

Any advice or help is appreciated!

H.Muster
  • 9,297
  • 1
  • 35
  • 46
Abi
  • 47
  • 3
  • You can do this by extracting individual digits from the number. Information on how to do it is given [here](http://www.mathworks.com/matlabcentral/answers/13751) – Autonomous Mar 09 '13 at 22:37

2 Answers2

3

Encoding may not be the best word to describe this. This answer is just a direction in your search.

I would have a look at any dictionary, hashtable or map data structures. Code below is based on solution described in this post

c = containers.Map;
c('0') = 3294; 
c('1') = 128;
c('2') = 2098;
keys(c) 
values(c)

You may need to add proper usings and maybe use integers types directly to remove casts and conversion overhead.

Community
  • 1
  • 1
oleksii
  • 35,458
  • 16
  • 93
  • 163
  • Awesome, thank you. I will look more into the containers.Map. What if for some of the values, I wanted to assign it to a function? For example, I had a table for integers 0 through 2, but then for integers greater than 2, I want to assign it to some function example(x) = 1- example(x-2)? – Abi Mar 10 '13 at 00:56
1

If you represent the encoding table as a cell array enc with two columns (each row contains an integer and its corresponding encoding string), you can use the following neat one-liner:

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

Explanation

The above solution actually does three things:

  1. Substitutes each integer in the input vector x with the corresponding encoding string from enc:

    arrayfun(@(v)enc{find([enc{:, 1}] == v), 2}', x(:), 'UniformOutput', 0)
    
  2. Concatenate all strings together into one column using cell2mat.

  3. Converts it back into a numerical vector using str2num. str2num is applied on rows, so each character (digit) is treated individually.

Example

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

The result is:

y =
     2     0     9     8     1     2     8     3     2     9     4
Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • Thank you for the insight! What if the values were 0s and 1s? When I tried to do enc = [2, 0111101] for example, it tells me enc = 2 111101 – Abi Mar 10 '13 at 01:16
  • @Abi Good point. The encoding table is numerical, and therefore leading zeros are omitted. I've fixed that (note that the encoding table `enc` is now a cell array). – Eitan T Mar 10 '13 at 09:06
  • Ah, I actually ended up doing switch cases instead. It was probably not very efficient (had almost 30 cases), but it works. Thank you again for the help though! – Abi Mar 13 '13 at 18:57