Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
doubt in matlab version in dec2bin() command

Subject: doubt in matlab version in dec2bin() command

From: PANDIAN` NITHYANANDAM

Date: 20 Jul, 2012 10:11:19

Message: 1 of 5


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


what is the reason; newImg2 is a double variable; even i type casted that in to uint8 data type; but still i cannot fiix this problem.

thanks in advance.
------------------------------------

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

Subject: doubt in matlab version in dec2bin() command

From: dpb

Date: 20 Jul, 2012 13:02:58

Message: 2 of 5

On 7/20/2012 5:11 AM, PANDIAN` NITHYANANDAM wrote:
...

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

> what is the reason;...

Looks like a bug in dec2bin.m to me...

Either change to use the '|' operator or use the form d(:) for the
arguments to bring them to the required scalar form for '||'

If the argument to dec2bin isn't a vector this will seemingly
happen--hard to imagine that getting by beta.

If you still have the earlier release, you might look at and compare the
code in the two versions m-files for dec2bin--there's nothing like that
in the version here but it's quite old so there's been stuff added since.

--

Subject: doubt in matlab version in dec2bin() command

From: dpb

Date: 20 Jul, 2012 13:19:35

Message: 3 of 5

On 7/20/2012 8:02 AM, dpb wrote:
...

>> 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))
> ....
>
>> what is the reason;...
>
> Looks like a bug in dec2bin.m to me...
>
> Either change to use the '|' operator or use the form d(:) for the
> arguments to bring them to the required scalar form for '||'
>
> If the argument to dec2bin isn't a vector this will seemingly
> happen--hard to imagine that getting by beta.
>
> If you still have the earlier release, you might look at and compare the
> code in the two versions m-files for dec2bin--there's nothing like that
> in the version here but it's quite old so there's been stuff added since.
...

Oh, I just noticed in my release that the problem would have been taken
care of by the line

d = d(:); % Make sure d is a column vector.

located very near the beginning of the dec2bin.m file just after some
gross inputs checking has succeeded. See if this has been removed or
accidently gotten moved into a conditional such that it isn't reliably
executed in the later version.

It would solve the above problem--however, w/o the rest of the code to
look at one can only presume introducing it again won't break something
else that has been modified...

--

Subject: doubt in matlab version in dec2bin() command

From: Steven_Lord

Date: 20 Jul, 2012 13:20:34

Message: 4 of 5



"dpb" <none@non.net> wrote in message news:jubkuf$u9u$1@speranza.aioe.org...
> On 7/20/2012 5:11 AM, PANDIAN` NITHYANANDAM wrote:
> ...
>
>> 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))
> ...
>
>> what is the reason;...
>
> Looks like a bug in dec2bin.m to me...

I wouldn't go that far, not quite yet.

To the original poster, what do these four lines of code display in the
Command Window when you execute each (one at a time) right before your
DEC2BIN call?

which -all any
which -all lt
which -all isfinite
which -all not

My suspicion is that you have an ANY function that is shadowing the built-in
ANY, and that function doesn't return a scalar logical value. Just to be
safe, though, I'd like to make sure that none of the other functions being
called on that line are shadowed either.

> Either change to use the '|' operator or use the form d(:) for the
> arguments to bring them to the required scalar form for '||'
>
> If the argument to dec2bin isn't a vector this will seemingly happen--hard
> to imagine that getting by beta.

It didn't. I just checked and DEC2BIN works perfectly fine on a matrix or a
vector.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: doubt in matlab version in dec2bin() command

From: dpb

Date: 20 Jul, 2012 18:48:43

Message: 5 of 5

On 7/20/2012 8:20 AM, Steven_Lord wrote:
...

>> Looks like a bug in dec2bin.m to me...
>
> I wouldn't go that far, not quite yet.
>
...

> My suspicion is that you have an ANY function that is shadowing the
> built-in ANY, and that function doesn't return a scalar logical value.
> Just to be safe, though, I'd like to make sure that none of the other
> functions being called on that line are shadowed either.
...

>> If the argument to dec2bin isn't a vector this will seemingly
>> happen--hard to imagine that getting by beta.
>
> It didn't. I just checked and DEC2BIN works perfectly fine on a matrix
> or a vector.

Yeah, you're more than likely correct & I should have double-checked the
aliasing as the first--it did seem very unlikely that such a faux-pas
could have slipped by...not sure why I didn't tell OP to do so--guess I
let the different versions cloud judgment there...

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us