-1

the conversion of decimal to binary of text derived from a file is

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);

output of this program if file contained '1' stored in it

THE MAGNITUDE OF THE TEXT IS =
49

 1

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

if the number of bit of x is 8bit then i want first 3 bits displayed in a variable and rest 5 bits in another variable i want a program for this in matlab.

eg x=00110011
a=001
b=10011 

encoding program

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';

the variable en should be split based on hop count same as u did with variable x.

15
  • What is this program supposed to compute? Could you give a sample i/p and o/p? Mar 25, 2013 at 17:03
  • this program reads a text or numeric from text file named 'one ' in E drive and displays the binary bits .
    – durga
    Mar 25, 2013 at 17:34
  • from the o/p i need to display a paticular number of bits from x . say if the user wants only first 3 bits , it should display first 3 bits of x and also if user wants 2 display last 4 bits. it shoud be dynamic from user how many bits he/she wants from x
    – durga
    Mar 25, 2013 at 17:40
  • So x is just a number array? How about x(1:3) and x(4:end)? Mar 25, 2013 at 17:51
  • So is that your entire question? Mar 25, 2013 at 17:55

2 Answers 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
0

Just slice the array:

disp(x(1:3));
disp(x(4:end));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.