MATLAB Answers

0

retrieve binary data back

Elysi Cochin さんによって質問されました 2019 年 3 月 17 日
最新アクティビティ Rik
さんによって コメントされました 2019 年 3 月 18 日
I have a binary data as
00 10 11 01 10 11 11 01
I need to replace this data as if
00 = 0
01 = 1
10 or 11 = *
if i get a new data as below
0 * * 1 * * * 1
can you suggest any way to get back the binary data
00 10 11 01 10 11 11 01

  1 件のコメント

Rik
2019 年 3 月 17 日
Because your mapping is not unique, this is not possible. There is no way for the reverser to know if you * was 10 or 11, so it will not be able to return a perfect reconstruction.
Also, cryptography is a bit of a tricky subject on this website, see the discussion here.

サインイン to comment.

タグ

3 件の回答

Rik
回答者: Rik
2019 年 3 月 18 日
 採用された回答

In case you wanted to accept my answer, I'll repost my code in an actual answer. However, I would suggest you use Madhan's solution. That one ignores the last bit if the length is odd (which might not be what you want), while my code throws an error.
data='0010110110111101';
replace_with={'00','0';'01','1';'10','*';'11','*'};
key_list=replace_with(:,1);
val_list=replace_with(:,2);
%check for even numbers:
if rem(numel(data),2)~=0
error('bits are not paired')
end
%convert to 1 pair per cell
datacell=mat2cell(data,1,2*ones(numel(data)/2,1));
% %loop through the replacer elements
% for n=1:numel(key_list)
% datacell(ismember(datacell,key_list{n}))=val_list(n);
% end
inds=cellfun(@(x) find_indices(x,key_list),datacell);
L= inds~=0;
datacell(L)=val_list(inds(L));
%convert back to a char array
result=cell2mat(datacell);
clc,disp(result)
%local function:
function inds=find_indices(element,key_list)
[~,inds]=ismember(element,key_list);
end

  3 件のコメント

Rik
2019 年 3 月 18 日
I mean that if you enter an odd number of bits, my code will throw an error:
data='011';
Error using Untitled (line 7)
bits are not paired
And Madhan's code will ignore the last bit and only process '01':
D =
'1'
Elysi Cochin 2019 年 3 月 18 日
Thank you sir
Sir is it possible to get back the data
data='0010110110111101'
from result = '0**1***1'
through any method
Rik
2019 年 3 月 18 日
No, that reversal is not possible, unless there is a known pattern to your data.

サインイン to comment.


madhan ravi
回答者: madhan ravi
2019 年 3 月 18 日
編集済み: madhan ravi
2019 年 3 月 18 日

c=strjoin(regexp(c,'\d{2}+','match')); % for safety measure
% ^----- your original binary data
Actual_datas={'00','01','10|11',' '};
Replace_with={'0','1','*',''};
D=regexprep(c,Actual_datas,Replace_with)

  0 件のコメント

サインイン to comment.


Image Analyst
回答者: Image Analyst
2019 年 3 月 17 日

Use strrep(). Try this:
str = '00 10 11 01 10 11 11 01'
% Make the length a multiple of three.
while rem(length(str), 3) ~= 0
str = [str, ' '];
end
% Make replacements
% 00 = 0
% 01 = 1
% 10 or 11 = *
str = strrep(str, '00 ', '0 ')
str = strrep(str, '01 ', '1 ')
str = strrep(str, '10 ', '* ')
str = strrep(str, '11 ', '* ')
% If desired, trim off any trailing or leading spaces.
str = strtrim(str)

  4 件のコメント

Image Analyst
2019 年 3 月 17 日
I'm not really 100% sure if there are actually spaces in it or if she just put them in to make it easier to visualize the pairs of digits. Maybe she'll say.
I find cellfun() is too cryptic for beginners, but if you have an example, please post it.
Rik
2019 年 3 月 17 日
Judging by the track record of Elysi I wouldn't call them a beginner anymore, but just in case other/future readers need an example (which assumes the spaces are not in the actual data):
data='0010110110111101';
replace_with={'00','0';'01','1';'10','*';'11','*'};
key_list=replace_with(:,1);
val_list=replace_with(:,2);
%check for even numbers:
if rem(numel(data),2)~=0
error('bits are not paired')
end
%convert to 1 pair per cell
datacell=mat2cell(data,1,2*ones(numel(data)/2,1));
% %loop through the replacer elements
% for n=1:numel(key_list)
% datacell(ismember(datacell,key_list{n}))=val_list(n);
% end
inds=cellfun(@(x) find_indices(x,key_list),datacell);
L= inds~=0;
datacell(L)=val_list(inds(L));
%convert back to a char array
result=cell2mat(datacell);
clc,disp(result)
%local function:
function inds=find_indices(element,key_list)
[~,inds]=ismember(element,key_list);
end
Elysi Cochin 2019 年 3 月 18 日
sorry sir, what you said is right. There was no spaces
I wrote like that to make it readable. I didnt think the other way. Sorry.
Rik Wisselink Sir your answer is what i wanted.

サインイン to comment.



Translated by