bitget function is not invoking output bits in a vector

2 visualizaciones (últimos 30 días)
Anmar Mohammed
Anmar Mohammed el 26 de Nov. de 2018
Editada: Anmar Mohammed el 26 de Nov. de 2018
k=k+1; %% k is counter and its chainging even more than (length(A))
for t = 1:1:length(A) %% A is a vector contains 8 elements
recovered_bits(1,k)= bitget(A(1,t),1); % extract LSB of each element in array and put them (LSB) in recovered_bits vector
k=k+1;
end
%% Output ----> recovered_bits = [] knowing that i trying to preallocate the vector previously
  3 comentarios
madhan ravi
madhan ravi el 26 de Nov. de 2018
Editada: madhan ravi el 26 de Nov. de 2018
See PREALLOCATION it improves code efficiency
Anmar Mohammed
Anmar Mohammed el 26 de Nov. de 2018
A is a vector contain 8 pixels consider it double

Iniciar sesión para comentar.

Respuesta aceptada

madhan ravi
madhan ravi el 26 de Nov. de 2018
Editada: madhan ravi el 26 de Nov. de 2018
You don't need a counter for this case , please pre-allocate before loop!
recovered_bits=cell(1,length(A)); %proper pre-allocation
for t = 1:length(A)
recovered_bits{t}= bitget(A(1,t),1);
end
[recovered_bits{:}]
  2 comentarios
Anmar Mohammed
Anmar Mohammed el 26 de Nov. de 2018
Editada: Anmar Mohammed el 26 de Nov. de 2018
Thanks for sharing, it really helped, the k is needed simply because the code above is invoked in a bigger problem within functions, thank you all for helping me, greetings.
madhan ravi
madhan ravi el 26 de Nov. de 2018
Anytime :), see Steven Lords answer which is the efficient way to do what you are doing

Iniciar sesión para comentar.

Más respuestas (2)

Steven Lord
Steven Lord el 26 de Nov. de 2018
Why loop?
r = randi([0 intmax('int8')], 8, 1, 'int8');
recovered_bits = bitget(r, 1);
result = [r, recovered_bits, mod(r, 2)]
The second and third columns of result should be the same.
  1 comentario
Anmar Mohammed
Anmar Mohammed el 26 de Nov. de 2018
Editada: Anmar Mohammed el 26 de Nov. de 2018
hi, am working on image steganography.. i used loop because i needed the first LSB or i may need the second or the third LSBs.. the output depend on other parameters with entire recieved image.. your code is intersting i tried to have the fist 3 LSBs using your modified code but an error ocured concerning input size, any suggestions are much preciated, thanks in advance.
r = randi([0 intmax('uint8')], 8, 1, 'uint8');
recovered_bits = bitget(r, 1:3,'uint8');
result = [r, recovered_bits]
Error using bitget
Inputs must have the same size.

Iniciar sesión para comentar.


KALYAN ACHARJYA
KALYAN ACHARJYA el 26 de Nov. de 2018
Editada: KALYAN ACHARJYA el 26 de Nov. de 2018
Initiallize k=1, otherwise K is not defined
k=1; %% k is counter and its chainging even more than (length(A))
for t = 1:1:length(A) %% A is a vector contains 8 elements
recovered_bits(1,k)= bitget(A(1,t),1); % extract LSB of each element in array and put them (LSB) in recovered_bits vector
k=k+1;
end
  2 comentarios
KALYAN ACHARJYA
KALYAN ACHARJYA el 26 de Nov. de 2018
Editada: KALYAN ACHARJYA el 26 de Nov. de 2018
For example I have choosen different A
A=[4 6 7 4 3 0 8 4];
for t=1:1:length(A) %% A is a vector contains 8 elements
recovered_bits(t)=bitget(A(1,t),1); % extract LSB of each element in array and put them (LSB) in recovered_bits vector
end
%% Output ----> recovered_bits = [] knowing that i trying to preallocate the vector previously
disp(recovered_bits);
Anmar Mohammed
Anmar Mohammed el 26 de Nov. de 2018
Oh.. am sorry it's already initialized and its K=1 not K=k+1 as wrote above, my bad.

Iniciar sesión para comentar.

Categorías

Más información sobre Desktop en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by