-1

从文件派生的文本的十进制到二进制的转换是

temp=textread('E:\one.txt', '%1s', 'whitespace', '');  
text = char(temp);                                                           
y = zeros(length(text)*8,1);                                           
for n = 1:1:length(text)  
a=abs(text(n));                                                              
f = 8*(n-1)+1;                                                          
y(f:f+7,1)=(de2bi(a,8))';                                                   
end
disp('THE MAGNITUDE OF THE TEXT IS =');
disp(a);
disp(f);
x=y';
disp('THE BINARY BITS ARE');
disp(x);

如果文件包含存储在其中的“1”,则此程序的输出

THE MAGNITUDE OF THE TEXT IS =
49

 1

THE BINARY BITS ARE
 1     0     0     0     1     1     0     0

如果 x 的位数是 8 位,那么我希望前 3 位显示在一个变量中,其余 5 位显示在另一个变量中,我想在 matlab 中为此编写一个程序。

eg x=00110011
a=001
b=10011 

编码程序

clc;
clear all;
temp=textread('E:\one.txt', '%1s', 'whitespace', '');  
text = char(temp);                                                               
y = zeros(length(text)*8,1);                                                
for n = 1:1:length(text)  
    a=abs(text(n));                                                                 
    f = 8*(n-1)+1;                                                                    
    y(f:f+7,1)=(de2bi(a,8))';                                            
end
disp('THE MAGNITUDE OF THE TEXT IS =');
disp(a);
disp(f);
x=y';
disp('THE BINARY BITS ARE');
disp(x);
z=length(x);
savefile='D:\mat\z.mat';
save (savefile,'z','-MAT');
disp('TOTAL NUMBER OF BITS =');
disp(z);
bk=input('ENTER THE NUMBER OF ROWS =');
savefile='D:\mat\bk.mat';
save (savefile,'bk','-MAT');
c=z/bk;
savefile='D:\mat\c.mat';
save (savefile,'c','-MAT');
k=1;
for i=1:bk
    for j=1:c
        m(i,j)=x(k);
        k=k+1;
    end
end
%disp(m(i,j));
disp('THE MESSAGE BITS ARE ');
disp(m);
savefile='D:\mat\m.mat';
save (savefile,'m','-MAT');
m_tot=(size(m,1)*size(m,2));
savefile='D:\mat\m_tot.mat';
save (savefile,'m_tot','-MAT');
savefile='D:\mat\r1.mat';
r1=[randperm(bk),randperm(bk)];
save (savefile,'r1','-MAT');
disp(r1);
savefile='D:\mat\r2.mat';
r2=[randperm(bk),randperm(bk)];
save (savefile,'r2','-MAT');
disp(r2);
savefile='D:\mat\f(1).mat';
f1= randint(1,1,[1,bk]);
save (savefile,'f1','-MAT');
savefile='D:\mat\en.mat';
en(1,:)=m(f1,:);
save (savefile,'en','-MAT');
disp('DIRECTLY ASSIGNED BLOCK IS');
disp(f1);
for w=1:(length(r1))
    en(w+1,:)=xor((m(r1(w),:)),(m(r2(w),:)));
    disp('THE EXORED BLOCKS ARE= ');
    disp(r1(w));
    disp(r2(w));
end
disp('THE ENCODED BITS ARE');
disp(en);
en_tot=(size(en,1)*size(en,2));
disp('tot no of encoded bits');
disp(en_tot);
save (savefile,'en_tot','-MAT');
savefile='D:\mat\en_tot.mat';

变量 en 应该根据跳数进行拆分,就像你对变量 x 所做的那样。

4

2 回答 2

0

Try this:

%After computing "x", the double array, as required...
d = input('Enter the length of the first sub-array: ');
a = x(1:d)
b = x(d+1:end)

For example:

>> 
temp='1';  
text = char(temp);                                                           
y = zeros(length(text)*8,1);                                           
for n = 1:1:length(text)  
a=abs(text(n));                                                              
f = 8*(n-1)+1;                                                          
y(f:f+7,1)=(de2bi(a,8))';                                                   
end
disp('THE MAGNITUDE OF THE TEXT IS =');
disp(a);
disp(f);
x=y';
disp('THE BINARY BITS ARE');
disp(x);
%After computing "x", the double array, as required...
d = input('Enter the length of the first sub-array: ');
a = x(1:d)
b = x(d+1:end)

The result would be:

THE MAGNITUDE OF THE TEXT IS =
    49

     1

THE BINARY BITS ARE
     1     0     0     0     1     1     0     0

Enter the length of the first sub-array: 3

a =

     1     0     0


b =

     0     1     1     0     0

I'm still unsure as to what this program is supposed to achieve though.

EDIT

New code as per changed requirement:

>> 
temp='1';  
text = char(temp);                                                           
y = zeros(length(text)*8,1);                                           
for n = 1:1:length(text)  
    a=abs(text(n));                                                              
    f = 8*(n-1)+1;                                                          
    y(f:f+7,1)=(de2bi(a,8))';                                                   
end
disp('THE MAGNITUDE OF THE TEXT IS =');
disp(a);
disp(f);
x=y';
disp('THE BINARY BITS ARE');
disp(x);
%After computing "x", the double array, as required...
d = input('Enter the hop-count vector: ');
for i=2:length(d)
    d(i) = d(i) + d(i-1);
end
d = [0, ceil((d./d(end))*length(x))];
disp('The resultant split up is:')
for i=2:length(d)
    disp(x((d(i-1)+1):d(i)));
end

The result will be:

THE MAGNITUDE OF THE TEXT IS =
    49

     1

THE BINARY BITS ARE
     1     0     0     0     1     1     0     0

Enter the hop-count vector: [3 2 3]
The resultant split up is:
     1     0     0

     0     1

     1     0     0
于 2013-03-25T18:00:16.027 回答
0

只需对数组进行切片:

disp(x(1:3));
disp(x(4:end));
于 2013-03-25T17:50:39.980 回答