matlab version problem in dec2bin function
Show older comments
hai matlab experts , this is a line of code in my matlab program
reqBinData = dec2bin(newImg2);
when i am run this snippet in matlab 7.6.0 R2008a , it does not give any error.
but when i run it in matlab 7.8.0.347 R2009a it gives the following error:
??? Operands to the and && operators must be convertible to logical scalar values.
Error in ==> dec2bin at 31 if any(d < 0) any(~isfinite(d)) reqBinData = dec2bin(newImg2);
Error in ==> sampic2abb at 122
------------------------------------
i have attached the entire code for the reference: ------------------------------------------------------------------------------
clear;
clc;
a = imread('file.bmp'); %Reading Input File
% stochastic resonance begins here
% D=size(a);
% M=D(1,1);
% N=D(1,2);
% a=double(a);
%
% %sigma=1;
% a=quant(a,4);
% a=a+randn(M,N);
% a=uint8(a);
% stochastic resonace ends here
b = bitand(a,240); %Extracting First 4 Bits from the MSB
% The following code will push 7,6,5,4th bit to right side; since i am not okay with rotation operatoin i did like this
for i = 1:size(a,1)
for j = 1:size(a,2)
if(b(i,j)== 240)
b1(i,j)=15;
elseif(b(i,j)== 224)
b1(i,j)=14;
elseif(b(i,j)== 208)
b1(i,j)=13;
elseif(b(i,j)== 192)
b1(i,j)=12;
elseif(b(i,j)== 176)
b1(i,j)=11;
elseif(b(i,j)== 160)
b1(i,j)=10;
elseif(b(i,j)== 144)
b1(i,j)=9;
elseif(b(i,j)== 128)
b1(i,j)=8;
elseif(b(i,j)== 112)
b1(i,j)=7;
elseif(b(i,j)== 96)
b1(i,j)=6;
elseif(b(i,j)== 80)
b1(i,j)=5;
elseif(b(i,j)== 64)
b1(i,j)=4;
elseif(b(i,j)== 48)
b1(i,j)=3;
elseif(b(i,j)== 32)
b1(i,j)=2;
elseif(b(i,j)== 16)
b1(i,j)=1;
elseif(b(i,j)== 0)
b1(i,j)=0;
end
end
end
% This is gray code coversion for 7,6,5,4th bit; since i am not okay with gray code operation i did like thi
% i am aware that both these operatoins inbuilt functions are available, but still i used
% logic for that
for i = 1:size(a,1)
for j = 1:size(a,2)
if(b1(i,j)== 15)
b2(i,j)=8;
elseif(b1(i,j)== 14)
b2(i,j)=9;
elseif(b1(i,j)== 13)
b2(i,j)=11;
elseif(b1(i,j)== 12)
b2(i,j)=10;
elseif(b1(i,j)== 11)
b2(i,j)=14;
elseif(b1(i,j)== 10)
b2(i,j)=15;
elseif(b1(i,j)== 9)
b2(i,j)=13;
elseif(b1(i,j)== 8)
b2(i,j)=12;
elseif(b1(i,j)== 7)
b2(i,j)=4;
elseif(b1(i,j)== 6)
b2(i,j)=5;
elseif(b1(i,j)== 5)
b2(i,j)=7;
elseif(b1(i,j)== 4)
b2(i,j)=6;
elseif(b1(i,j)== 3)
b2(i,j)=2;
elseif(b1(i,j)== 2)
b2(i,j)=3;
elseif(b1(i,j)== 1)
b2(i,j)=1;
elseif(b1(i,j)== 0)
b2(i,j)=0;
end
end
end
c = bitand(a,15); %Extracting Last 4 Bits from the LSB
n = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
cntval = n;
disp('Size of the Image: ')
disp(size(a));
for i = 1:size(a,1)
for j = 1:size(a,2)
n(c(i,j)+1) = n(c(i,j)+1) + 1;
end
end
maxn = max(n);
disp('0\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12\t13\t14\15');
disp(n);
disp('Total Value of the Pixels from 0 to 7');
disp(sum(n(:)));
for i = 1:16
if n(i) ~= maxn
n(i) = 0;
end
end
indn = find(n);
indn = indn - 1; %Index Stores the most frequently occuring value in the last 4 bits
disp('Maximum Value Index');
disp(indn);
newImg2 = b2;
reqBinData = dec2bin(newImg2);
% reqBinData1 = dec2bin(b);
reqBinData = reqBinData';
singlArr = [reqBinData(4,:),reqBinData(3,:),reqBinData(2,:),reqBinData(1,:)];
dlmwrite('1.txt',singlArr, 'delimiter',' ','newline','pc');
reqMetrics = [size(a), indn]; %Metrics Required to Decode the file
disp(reqMetrics);
dlmwrite('metrics.txt',reqMetrics, 'delimiter',' ','newline','pc');
1 Comment
Ryan
on 20 Jul 2012
What is the size/class off newImg2/b2? Does it contain negative or non-integers?
Answers (1)
Walter Roberson
on 20 Jul 2012
0 votes
In some releases, dec2bin() does not accept arrays but does accept vectors.
In my opinion, failing on arrays is not a bug, since dec2bin is only documented to work on scalars. But Mathworks counted it as a bug and fixed it in later releases.
The workaround is to reshape the array into a vector.
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!