4 views (last 30 days)

Hi guys,

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

this function does the binary converstion according to to the attached table, for example:

binrestore(1,[0 1]) ----> output= [0 1] . because 0 is mapped to zero according to table at m=1, and 1 is mapped to one.

binrestore(2,[0 1]) ----> output= [0 0 1 1 ]. because 0 is mapped to 0 0 and 1 is mapped to 1 1 according to table.

binrestore(4,[0 1]) ----> output= [1 1 0 0 0 0 1 1]. because 0 is mapped to 1 1 0 0 and 1 is mapped to 0 0 1 1 according to table.

what Im trying to implement is the reverse of this function, it means the output of this function is my input to the function that I want to implement which it's called reversebinrestore(m,array) and its output is the input to my function above.

for example of the function that Im trying to implement, If I input to it:

reversebinrestore(1,[0 1]) --> Output=[0 1]

reversebinrestore(2, [0 0 1 1 ]) --> Output=[0 1]

reversebinrestore(4,[1 1 0 0 0 0 1 1]) ---> Output=[0 1] , this because [1 1 0 0] according to table is mapped to zero, and [0 0 1 1 ] is mapped to one.

..etc

exactly the same functionality of binrestore(m ,array) but in opposite/reverse.

Any help please how I can I implement that in matlab? thanks alot!

David Hill
on 6 Sep 2020

function ret=reversebinrestore(m,array)

ret=[];

switch m

case 1

l=[0;1];

for k=1:length(array)

ret=[ret,find(ismember(l,array(k)))-1];

end

case 2

l=[0 0;1 1];

array=reshape(array,2,[])';

for k=1:size(array,1)

ret=[ret,find(ismember(l,array(k,:),'rows'))-1];

end

case 4

l=[1 1 0 0;0 0 1 1];

array=reshape(array,4,[])';

for k=1:size(array,1)

ret=[ret,find(ismember(l,array(k,:),'rows'))-1];

end

case 8

l=[1 1 0 0 1 1 0 0;0 0 1 1 0 0 1 1];

array=reshape(array,8,[])';

for k=1:size(array,1)

ret=[ret,find(ismember(l,array(k,:),'rows'))-1];

end

end

end

Bruno Luong
on 6 Sep 2020

function [output, outputchar] = reversebinrestore(m, array)

if ischar(array)

array = array-'0';

end

switch m

case 1

map0 = [0];

case 2

map0 = [0 0];

case 4

map0 = [1 1 0 0];

case 8

map0 = [1 1 0 0 1 1 0 0];

otherwise

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

end

map1 = 1-map0;

map = [map0; map1];

array = reshape(array, size(map,2), []).';

[b,output] = ismember(array, map, 'rows');

if ~all(b)

error('invalid array');

end

output = output(:)'-1;

outputchar = char(output+'0');

end

Bruno Luong
on 6 Sep 2020

If you need only once just call with once.

In YOUR code you returns a char array

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

Then in your description youu said

"output= [0 1 ]"

So I don't know which format you decide to use so I make both available.

Decide then stick on what you have decided. This appply on data format, function name, filename, etc...

You seem to hesitate a lot.

Bruno Luong
on 6 Sep 2020

>> xxx(2,[0 1])

ans =

0 0 1 1

>> out = xxx(2,[0 1])

out =

0 0 1 1

>> [out, outstr] = xxx(2,[0 1])

out =

0 0 1 1

outstr =

'0011'

>> [~, outstr] = xxx(2,[0 1])

outstr =

'0011'

>> [~, outstr] = xxx(2,'01')

outstr =

'0011'

>>

Opportunities for recent engineering grads.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/589654-reversing-binary-stream-conversion-according-to-attached-table#comment_998632

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/589654-reversing-binary-stream-conversion-according-to-attached-table#comment_998632

Sign in to comment.