Reversing binary stream Conversion According to attached table?
Show older comments
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!
Answers (2)
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
5 Comments
David Hill
on 6 Sep 2020
You are inputting the wrong thing. Your inputs are not in accordance with your description. It works perfectly for me if you input the correct thing.
reversebinrestore(2, [0 0 1 1 ]);
reversebinrestore(4,[1 1 0 0 0 0 1 1]);
reversebinrestore(2,[0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 1 1 1 1 0 0]);
reversebinrestore(8, [0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 ]);
Jimmy cho
on 6 Sep 2020
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
8 Comments
Bruno Luong
on 6 Sep 2020
You wrote in the question (I quote without changing a single word)
"binrestore(2,[0 1]) ----> output= [0 0 1 1 ]"
"for example of the function that Im trying to implement, If I input to it:
...
reversebinrestore(2, [0 0 1 1 ]) --> Output=[0 1]"
Now if you said "you didn't understand me" then "you didn't understand you" since we just did exactly what you request in this question:
"reversebinrestore(2, [0 0 1 1 ]) --> Output=[0 1]"
>> reversebinrestore(2, [0 0 1 1 ])
ans =
0 1
Jimmy cho
on 6 Sep 2020
Bruno Luong
on 6 Sep 2020
Edited: Bruno Luong
on 6 Sep 2020
I let you sortout the filename xxx, yyy you seem to describe them one way or another depending on your mood.
function map = getmap(m)
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];
end
Then xxx
function [output, outputchar] = xxx(m, array)
if ischar(array)
array = array-'0';
end
map = getmap(m);
output = map(array+1,:);
output = reshape(output',1,[]);
outputchar = char(output+'0');
end
Then yyy
function [output, outputchar] = yyy(m, array)
if ischar(array)
array = array-'0';
end
map = getmap(m);
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
These is examples of what xxx and yyy do, one is the reverse of the other. Rename it as your mood tells.
>> yyy(2,[0 0 1 1])
ans =
0 1
>> xxx(2,[0 1])
ans =
0 0 1 1
>>
Jimmy cho
on 6 Sep 2020
Bruno Luong
on 6 Sep 2020
Edited: 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.
Jimmy cho
on 6 Sep 2020
Bruno Luong
on 6 Sep 2020
Edited: Bruno Luong
on 7 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'
>>
Categories
Find more on Axis Labels in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!