4 views (last 30 days)

Hello .

Im struggling to implement a function in matlab that restore stream of binary values according to specific scheme that Im attaching a photo for it:

my function called restore (or whatever you'd call it it's fine for me) and have two inputs, one input called arr, second input is a variable/parameter called multiple.

first input is arr and it's a binary values like arr=[1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,1,1,0,0] ;

second input as what shown in my photo above it's a parametr that its values are one of those values [1 , 2 , 4, 8] , so for me I called this as dictionary.

what my function does is restoring the binary values according to the attached dictionary, this means if my multiple is 1 then if in my input arr there's a value 0 or 1 it will be mapped back as 0 or 1 respectively (see the value of multiple=1 on the photo above you will see in the green color the value of the bit and according to value of multiple I store back the values of arr). i.e we look first at the given value of parameter multiple then according to mape the binary values as showen in the dictionary.

Scheme of mapping diagram:

multiple Mapped Value = 0 Mapped Value= 1

1 0 1

2 00 11

4 1100 0011

8 11001100 00110011

the output is a binary array values according to the scheme of mapping attached it above.

the value of multiple implicitly represents the number of values or bits of the input array (arr) that I want to check every time for mapping. reading array value from LEFT to Right.

examples for more clarifications:

E.1:

multiple=1, arr=[1,0,1,0,1,0] , when multiple =1 we check according to the dictionary table that attached above one bit separately

function returns in my case a binary integers array which it's output=[101010] -we see on the dictionary table above that if arr[i] (i is index) has value 1 then the mapped value is one, if arr[i]=0 then the mapped value is zero, i.e 1 ->1 ; 0 -> 0.

E.2:

multiple =2, arr=[0,0,1,1,1,1,0,0] , when multiple =2 we check according to the dictionary table that attached above two bit separately

we see on the dictionary when multiple =2 then according to dictionary scheme attached above we see 00 -> 0 ; 11 ->1 this means in my case:

function returns a binary integers array which it's output=[0110].

E.3:

multiple =4, arr=[1,1,0,0,0,0,1,1,0,0,1,1,0,0,1,1,1,1,0,0,0,0,1,1] , when multiple =4 we check according to the dictionary table that attached above four bit separately every time.

we see on the dictionary when multiple =4 then according to dictionary scheme that attached above we see 1100 -> 0 ; 0011 ->1 this means in my case:

function returns a binary integers array which it's output=[0,1,1,1,0,1]. I will clear why the output is like this:

we look every time from left to right on the values of arr and we check 4values every time.

we see first 1100 -> 0, next following 0011 -> 1, next following 0011 -> 1, next following 0011 -> 1, next following 1100 -> 0 , last next four following data 0011->0.

so the mapped output is output=[0,1,1,1,0,0].

E.4:

multiple =8, arr=[1,1,0,0,1,1,0,0,0,0,1,1,0,0,1,1] , when multiple =8 we check according to the dictionary table that attached above 8bit separately every time.

we see on the dictionary when multiple =4 then according to dictionary scheme that attached above we see 11001100 -> 0 ; 00110011 ->1 this means in my case:

function returns a binary integers array which it's output=[01]. I will clear why the output is like this:

we look every time from left to right on the values of arr and we check 4values every time.

we see first 11001100 -> 0, next following 00110011 -> 1.

so the mapped output as we see mapped to output=[0,1].

the function gets two inputs function restor(arr,multiple) and returns as what I explained above the mapped binary array values according to dictionary scheme on the photo.Moreover, there's input correctness this means that can't be interleve between schemes, so if multiple equals specific value of its possibilites then the input follows just this multiple value scheme and can't interleve to another value scheme (there's no overlapping between the schemes of each multiple value), also If multiple isn't equal to one of its range values [1 2 4 8] then the function returns exception or null or any notation that multiple isn't on the range.

Notifying that the output isn't a string array, it's binary integer array.

I hope my problem is much elaborated and understood for you, Could anyone help me how I do implement that in matlab?

Appreciated in advance for your cooperation.

dpb
on 24 Aug 2020

Edited: dpb
on 24 Aug 2020

function ret=binrestore(m,array)

% returns binary decoded array values from input encoded array

% multiple Mapped Value = 0 Mapped Value= 1

% 1 0 1

% 2 00 11

% 4 1100 0011

% 8 11001100 00110011

assert(mod(numel(array),m)==0,'Input array length not multiple of multipler')

array=reshape(array,m,[]); % separate out digits to compare

switch m

case 1 % nothing else to do at this point

case 2

array=all(array==1);

case 4

array=all(array==[0 0 1 1].');

case 8

array=all(array==[0 0 1 1 0 0 1 1].');

otherwise

error('Multiplier not 1, 2, 4, 8')

end

ret=num2str(array,'%d');

end

function ret=binrestore(m,array)

% returns binary decoded array values from input encoded array

% multiple Mapped Value = 0 Mapped Value= 1

% 1 0 1

% 2 00 11

% 4 1100 0011

% 8 11001100 00110011

assert(mod(numel(array),m)==0,'Input array length not multiple of multipler')

if m==1

ret=array;

return

end

lookup={1,[1 1].',[0 0 1 1].',[0 0 1 1 0 0 1 1].'};

n=log2(m)+1;

array=reshape(array,m,[]); % separate out digits to compare

array=all(array==lookup{n});

ret=double(array);

end

dpb
on 6 Sep 2020

Opportunities for recent engineering grads.

Apply TodayFind the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 0 Comments

Sign in to comment.