Reversing binary stream Conversion According to attached table?

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!

1 件のコメント

Jimmy cho
Jimmy cho 2020 年 9 月 6 日
Could be much appreciated if you could give me aid.

サインインしてコメントする。

回答 (2 件)

David Hill
David Hill 2020 年 9 月 6 日

1 投票

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 件のコメント

Jimmy cho
Jimmy cho 2020 年 9 月 6 日
編集済み: Jimmy cho 2020 年 9 月 6 日
Hi !
Thanks for your help but your code isn't giving me correct outputs.
I tried in my command matlab window:
>> reversebinrestore(2,[0 1])
ans =
0×1 empty double column vector
which it must be the output= [0 0 1 1 ]
also I tried:
reversebinrestore(4,[0 1])
Error using reshape
Product of known dimensions, 4, not divisible into total number of elements, 2.
Error in reversebinrestore (line 17)
array=reshape(array,4,[])';
if I input array [0 1] with m=2 for instance, then 0 is mapped according to table to [0 0] and 1 is mapped to [1 1] so the output is [0 0 1 1]
if I input array [0 1 1] with m=4 for instance then, 0 is mapped according to table to [1 1 0 0] and 1 is mapped to [0 0 1 1 ]
so the output is [1 1 0 0 0 0 1 1 0 0 1 1 0 0 1 1 ];
David Hill
David Hill 2020 年 9 月 6 日
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
Jimmy cho 2020 年 9 月 6 日
編集済み: Jimmy cho 2020 年 9 月 6 日
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 2020 年 9 月 6 日
編集済み: Jimmy cho 2020 年 9 月 6 日
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 2020 年 9 月 6 日
should I edit again my thread or it's understandable? sorry for my missunderstandings.

サインインしてコメントする。

Bruno Luong
Bruno Luong 2020 年 9 月 6 日

1 投票

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 件のコメント

Bruno Luong
Bruno Luong 2020 年 9 月 6 日
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
Jimmy cho 2020 年 9 月 6 日
編集済み: Jimmy cho 2020 年 9 月 6 日
Hi once again, my bad!
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 2020 年 9 月 6 日
should I edit again my thread or it's understandable? sorry for my missunderstandings.
Bruno Luong
Bruno Luong 2020 年 9 月 6 日
編集済み: Bruno Luong 2020 年 9 月 6 日
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
Jimmy cho 2020 年 9 月 6 日
100% !
but for function xxx, why do I need two outputs ? I mean
function [output, outputchar] = xxx(m, array)
the variable called output is the array output of my function, but what about the second output called outputchar .. do I need it?!
because "ans" is actually the output array of function xxx that's stored on variable array called "output" ..am I right?!
thanks alot for any clarification on the code
Bruno Luong
Bruno Luong 2020 年 9 月 6 日
編集済み: Bruno Luong 2020 年 9 月 6 日
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 2020 年 9 月 6 日
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 2020 年 9 月 6 日
編集済み: Bruno Luong 2020 年 9 月 7 日
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'
>>

サインインしてコメントする。

カテゴリ

質問済み:

2020 年 9 月 6 日

編集済み:

2020 年 9 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by