How to return in Matlab specific array according to certain scheme?

3 ビュー (過去 30 日間)
Jimmy cho
Jimmy cho 2020 年 8 月 24 日
コメント済み: dpb 2020 年 9 月 6 日
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
dpb 2020 年 8 月 24 日
編集済み: dpb 2020 年 8 月 24 日
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
  6 件のコメント
Jimmy cho
Jimmy cho 2020 年 9 月 5 日
編集済み: Jimmy cho 2020 年 9 月 5 日
@dpb
Hi @dpb
Im trying to do the same concept of what your code does but in opposite, it means:
if I input to a function called (this is the function that Im trying to implement in matlab)
function ret=reversebinrestore(m,array)
m=1 , and array is [1 0]
then output is [1 0]
if m=2, and array = [1 0] then the output is [1 1 0 0] according to attached mapping table.
if m=4, and array =[1 0 1] then the output is [0 0 1 1 1 1 0 0 0 0 1 1] according to attached mapping table.
if m=8, and array=[1 1 0 0] then the output is [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 ];
etc ..
Im still using the same table of mapping :
% multiple Mapped Value = 0 Mapped Value= 1
% 1 0 1
% 2 00 11
% 4 1100 0011
% 8 11001100 00110011
Im trying to implement the same concept of what your attached code above does but in reverse ( it means the compatible opposite output of what your attached code does ... I mean by this the output of your attached code above is my input to my reversebinrestore(m,array) and the input of your attached code above is my output of the function that I want to implement)
Could you please help me how could I do the reverse function in matlab? appreciated !
thanks alot.
dpb
dpb 2020 年 9 月 6 日
This is basically just stitching together the right output string in order -- there's only two choices and you've already got an indexing vector passed as the input. You just use the right output string based on the code length. Again, of course, you special-case m==1.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by