HOW TO ENCODE AN 8BIT VALUE FROM A MATRIX

HELLO
I have generated a 256x256 matrix with an 8bit binary sequence present in each element. I wish to encode the sequence using the following scheme
A=00
B=01
C=10
D=11
i need to encode the whole matrix.please help.the matrix generated is from the following code
clc
clear all
close all
a=imread('C:\Users\Abzz\Desktop\lena.png');
imshow(a)
disp(a)
for i=1:1:256
for j=1:1:256
b{i,j,1} = dec2bin(a(i,j),8);
end
end
disp(b)
M=randint(256,256,[0,256]);
disp(M)
for k=1:1:256
for l=1:1:256
N{k,l,1} = dec2bin(M(k,l),8);
end
end
disp(N)
thanks in advance

 Risposta accettata

Hello, Check this. I think it would work for you. It worked for me fine.
for i=1:size(b,1)
for j=1:size(b,2)
dum=0;
for k=1:2:size(b,3)
dum=dum+1;
if (b(i,j,k)*10+b(i,j,k+1))==00
test(i,j,dum)='A';
elseif (b(i,j,k)*10+b(i,j,k+1))==01
test(i,j,dum)='B';
elseif (b(i,j,k)*10+b(i,j,k+1))==10
test(i,j,dum)='C';
else
test(i,j,dum)='D';
end
end
end
end
Please let me know in case of further doubts.
Regards, Pratik

5 Commenti

Abirami
Abirami il 14 Ago 2014
Modificato: Abirami il 14 Ago 2014
i dont have the 4bit char sequence in place of the 8bit elements....pls help...thanks in advance..
Could you please elaborate the question?...
I didn't get it properly. Basically, the code that i just posted converts all the 256x256x8 sequences into 256x256x4 seqeunces, where '00' in the former matrix is converted into encoding of 'A' and so on. So, your 8bit sequences are encoded into 4char sequences according to the above mentioned encoding.
Please let me know if i have got it correct.
Abirami
Abirami il 14 Ago 2014
Modificato: Abirami il 14 Ago 2014
yes sir it is the same...but when i try to run the program it shows two columns and four rows of single binary bits alone and no characters...what i want is to convert the 8bit sequences into 4bit characters.. eg:if 10110001 then i want it as CDAB and it should be done for the entire matrix...thanks in advance....
Ok Let me use altogether new concept to get it done. Used a lot of times in C/C++, the idea of structures.
So use the following code, once you are done executing the above code, I posted and converted them to a 256x256x4 Char Matrix.
for i=1:size(b,1)
for j=1:size(b,2)
test_encode_dummy=test(i,j,1);
for k=2:size(test,3)
test_encode_dummy=cat(2,test_encode_dummy,test(i,j,k));
end
test_encode(i,j).code=test_encode_dummy;
end
end
That's it!
Hope it helps.
Regards, Pratik
Abirami
Abirami il 15 Ago 2014
Modificato: Abirami il 15 Ago 2014
sir, im not able to follow...i tried but im getting the following error
Attempted to access b(1,1,2); index out of bounds because size(b)=[256,2048,1].
Error in ==> prat1 at 20 if (b(k,l,m)* 10+b(k,l,m+1))==00
also im not able to view the result...pls help..thanks in advance

Accedi per commentare.

Più risposte (1)

M=randint(256,256,[0,256]);
disp(N)
N=zeros(size(M));
for k=1:256
for l=1:256
N(k,l) = str2double(dec2bin(M(k,l),8));
end
end

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by