MATLAB Answers

Reversing binary stream Conversion According to attached table?

4 views (last 30 days)
Jimmy cho
Jimmy cho on 6 Sep 2020
Edited: Bruno Luong on 7 Sep 2020
Hi guys,
best regard for @dpb to help me to built this function in matlab:
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
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

Show 2 older comments
Jimmy cho
Jimmy cho on 6 Sep 2020
Hi, I appreciate your effort , but you didn't understand me, the function above (attached code) is exactly I've done it and it's using the same thing as what you've done.
what Im trying to do is exactly the opposite, it means if I input [0 1] with m=1 then output is [0 1]
if I input [0 1] and m=2 then output is [0 0 1 1 ] ..etc
exactly that's what I called it "reverse" of my function above(reverse of what my attached code above does..)
Could you please help me how to implement the reverse function in matlab? once again sorry for the missunderstanding in my thread, and exactly what you're inputting is my output and the output of what you've coded is my input (the compatible binary stream).
Once again, I want to implement the function that it does the same thing as what you've done but in reverse, so if I input [0 1] with m=2, reversebinrestore(2, [0 1]); then output is [ 0 0 1 1] according to attached table.
Jimmy cho
Jimmy cho on 6 Sep 2020
Omg!
Sorry for this, you're right , I just messed it up.
thanks alot , didn't notice omg!
Im editing what I wrote:
You wrote in the question (I quote without changing a single word)
"binrestore(2,[0 0 1 1]) ----> output= [0 1 ]
"for example of the function that Im trying to implement, If I input to it:
...
reversebinrestore(2, [0 1 ]) --> Output=[0 0 1 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 1 ]) --> Output=[0 0 1 1]"
""reversebinrestore(4, [0 1 ]) --> Output=[1 1 0 0 0 0 1 1]"
the output is accordingly to the table ..
Now is it understandable? your code is works fine but not the required output, my bad sorry!
So my reversebinrestore does as what I mentioned here above (the reverse ..)
Jimmy cho
Jimmy cho on 6 Sep 2020
should I edit again my thread or it's understandable? sorry for my missunderstandings.

Sign in to comment.


Bruno Luong
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

Show 5 older comments
Bruno Luong
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
Jimmy cho on 6 Sep 2020
I understand you!
but in the case function [output, outputchar] = xxx(m, array)
it returns just an array, doesn't return two @outputs!
So maybe Im missing something? I mean if I write xxx(1 , [0 1])
it must return in your case two outputs one for output , the other for outputcar
but once I write on command window xxx(1 , [0 1]) it just returns one output and it's an array of integers and not two @outputs although it should return output , outputchar.
could you explain why?!
thanks alot for your help
Bruno Luong
Bruno Luong on 6 Sep 2020
Depend how you call it. This is basic MATLAB calling syntax and mmore advanced ignore output
>> 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'
>>

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by