从文件派生的文本的十进制到二进制的转换是
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 所做的那样。