bitget function is not invoking output bits in a vector
조회 수: 6 (최근 30일)
이전 댓글 표시
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
채택된 답변
madhan ravi
2018년 11월 26일
편집: madhan ravi
2018년 11월 26일
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
madhan ravi
2018년 11월 26일
Anytime :), see Steven Lords answer which is the efficient way to do what you are doing
추가 답변 (2개)
Steven Lord
2018년 11월 26일
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
KALYAN ACHARJYA
2018년 11월 26일
편집: KALYAN ACHARJYA
2018년 11월 26일
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
KALYAN ACHARJYA
2018년 11월 26일
편집: KALYAN ACHARJYA
2018년 11월 26일
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/196942/image.png)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!