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:
How to convert a logical to double

Subject: How to convert a logical to double

From: Jordan Rosenthal

Date: 14 Dec, 2000 12:15:25

Message: 1 of 12

Hi all,

Another thread started me thinking about this:

How does one convert a logical array back to an array of doubles without
using:

   a_logical = logical( [ 1 2 3 ] );
   a_double = a_logical + 0;

Is seems to me that this is inefficient. Why should you need to add a value
(even if it is zero) to every element in a vector just to change from
logical to double. I would have thought that:

   a_double = double( a_logical )

would work, but it doesn't.

Jordan

Subject: How to convert a logical to double

From: jacklam@math.uio.no (Peter J. Acklam)

Date: 14 Dec, 2000 18:59:22

Message: 2 of 12

"Jordan Rosenthal" <jr@ece.gatech.edu> writes:

> How does one convert a logical array back to an array of doubles without
> using:
>
> a_logical = logical( [ 1 2 3 ] );
> a_double = a_logical + 0;
>
> Is seems to me that this is inefficient. Why should you need to
> add a value (even if it is zero) to every element in a vector
> just to change from logical to double.

You don't need to. You can use

   a_double = +a_logical;

Peter

--
function f,f=fopen(which(mfilename));.......................Just
while ~feof(f),l=fgetl(f);fprintf(.......................another
'%s ',l(1+max(find(l=='.')):end)),........................Matlab
end,fclose(f);fprintf('\n')%..............................hacker

Subject: How to convert a logical to double

From: Jordan Rosenthal

Date: 14 Dec, 2000 13:25:38

Message: 3 of 12

Peter,

That calls the UPLUS function instead of the PLUS function. But does this
just change the logical flag for the variable (which is what I'm looking
for) or does it do something else as well? I'm thinking there must be
something else going in the internals of Matlab, because if I use -a_logical
it changes the sign of all the elements.

Jordan

"Peter J. Acklam" <jacklam@math.uio.no> wrote in message
news:wkhf46opdx.fsf@math.uio.no...
> "Jordan Rosenthal" <jr@ece.gatech.edu> writes:
>
> > How does one convert a logical array back to an array of doubles without
> > using:
> >
> > a_logical = logical( [ 1 2 3 ] );
> > a_double = a_logical + 0;
> >
> > Is seems to me that this is inefficient. Why should you need to
> > add a value (even if it is zero) to every element in a vector
> > just to change from logical to double.
>
> You don't need to. You can use
>
> a_double = +a_logical;
>
> Peter
>
> --
> function f,f=fopen(which(mfilename));.......................Just
> while ~feof(f),l=fgetl(f);fprintf(.......................another
> '%s ',l(1+max(find(l=='.')):end)),........................Matlab
> end,fclose(f);fprintf('\n')%..............................hacker

Subject: How to convert a logical to double

From: jacklam@math.uio.no (Peter J. Acklam)

Date: 14 Dec, 2000 19:50:27

Message: 4 of 12

"Jordan Rosenthal" <jr@ece.gatech.edu> writes:

> That calls the UPLUS function instead of the PLUS function. But
> does this just change the logical flag for the variable (which
> is what I'm looking for) or does it do something else as well?
> I'm thinking there must be something else going in the internals
> of Matlab, because if I use -a_logical it changes the sign of
> all the elements.

I don't know much about the internals of Matlab, but I do know
that any arithmetic operation removes the logical flag, and
although the unary plus operator is an arithmetic operator, it
performs no arithmetic operations, so it is a lot faster than
adding zero to all elements.

Peter

--
function f,f=fopen(which(mfilename));.......................Just
while ~feof(f),l=fgetl(f);fprintf(.......................another
'%s ',l(1+max(find(l=='.')):end)),........................Matlab
end,fclose(f);fprintf('\n')%..............................hacker

Subject: How to convert a logical to double

From: Jordan Rosenthal

Date: 14 Dec, 2000 13:57:24

Message: 5 of 12

Fair enough....thanks.

Jordan

"Peter J. Acklam" <jacklam@math.uio.no> wrote in message
news:wkvgsmltvw.fsf@math.uio.no...
> "Jordan Rosenthal" <jr@ece.gatech.edu> writes:
>
> > That calls the UPLUS function instead of the PLUS function. But
> > does this just change the logical flag for the variable (which
> > is what I'm looking for) or does it do something else as well?
> > I'm thinking there must be something else going in the internals
> > of Matlab, because if I use -a_logical it changes the sign of
> > all the elements.
>
> I don't know much about the internals of Matlab, but I do know
> that any arithmetic operation removes the logical flag, and
> although the unary plus operator is an arithmetic operator, it
> performs no arithmetic operations, so it is a lot faster than
> adding zero to all elements.
>
> Peter
>
> --
> function f,f=fopen(which(mfilename));.......................Just
> while ~feof(f),l=fgetl(f);fprintf(.......................another
> '%s ',l(1+max(find(l=='.')):end)),........................Matlab
> end,fclose(f);fprintf('\n')%..............................hacker

Subject: How to convert a logical to double

From: us

Date: 14 Dec, 2000 23:32:10

Message: 6 of 12

hi jordan
i do understand your point!
however, why should
double(a_logical)
release (a potentially added) logical flag, rather than just changing
its bit-size/precision from whatever to a <type double>; in some ways
this would make the <double> -caster interfere with something entirely
different ...
e.g.,
we often use
lvar=uint8(logical(...))
for mem reasons, being aware that
lvar=-1;
will have an <unpleasant> side effect (e.g., -1 => FALSE_INDEX).
now, at some stage somebody might come along and decide that <-1> could
be a value that <lvar> should be able to digest (e.g., -1 =>
TRUE_INDEX). hence, she just type-casts <lvar> to a <double> and would
not have to be concerned about it's other flag(s).

also,
help logical
is very clear on how to get rid of the flag:
+a_logical
which comes without computational penalty.

just a note,
us


In article <91av64$hbd$1@news-int.gatech.edu>,
  "Jordan Rosenthal" <jr@ece.gatech.edu> wrote:
> Hi all,
>
> Another thread started me thinking about this:
>
> How does one convert a logical array back to an array of doubles
without
> using:
>
> a_logical = logical( [ 1 2 3 ] );
> a_double = a_logical + 0;
>
> Is seems to me that this is inefficient. Why should you need to add
a value
> (even if it is zero) to every element in a vector just to change from
> logical to double. I would have thought that:
>
> a_double = double( a_logical )
>
> would work, but it doesn't.
>
> Jordan
>
>


Sent via Deja.com
http://www.deja.com/

Subject: How to convert a logical to double

From: jacklam@math.uio.no (Peter J. Acklam)

Date: 15 Dec, 2000 01:28:36

Message: 7 of 12

us <us@neurol.unizh.ch> writes:

> help logical
> is very clear on how to get rid of the flag:
> +a_logical

Yes, but this was changed from R11 to R12. The old R11 docs said
that the logical flag could be removed by adding a zero.

Peter

--
function f,f=fopen(which(mfilename));.......................Just
while ~feof(f),l=fgetl(f);fprintf(.......................another
'%s ',l(1+max(find(l=='.')):end)),........................Matlab
end,fclose(f);fprintf('\n')%..............................hacker

Subject: How to convert a logical to double

From: us

Date: 15 Dec, 2000 02:32:45

Message: 8 of 12

you're correct: one should always mention the ver used (especially
during such tedious transitional times 5.x -> 6.x).
us
sys: i586/w2k.sp1/r12

In article <wkr93awmrv.fsf@math.uio.no>,
  jacklam@math.uio.no (Peter J. Acklam) wrote:
> us <us@neurol.unizh.ch> writes:
>
> > help logical
> > is very clear on how to get rid of the flag:
> > +a_logical
>
> Yes, but this was changed from R11 to R12. The old R11 docs said
> that the logical flag could be removed by adding a zero.
>
> Peter
>
> --
> function f,f=fopen(which(mfilename));.......................Just
> while ~feof(f),l=fgetl(f);fprintf(.......................another
> '%s ',l(1+max(find(l=='.')):end)),........................Matlab
> end,fclose(f);fprintf('\n')%..............................hacker
>


Sent via Deja.com
http://www.deja.com/

Subject: How to convert a logical to double

From: Jordan Rosenthal

Date: 15 Dec, 2000 10:11:30

Message: 9 of 12

Us,

> > > is very clear on how to get rid of the flag:

Even though I have R12 installed, I wind up using R11 more often when
thinking about newsgroup questions because it loads up much quicker and I
don't tend to keep Matlab running very long. For longer sessions, I usually
use R12 because I like the environment better.

Anyway, I guess my conclusion from all this is that, ideally, there should
be a function called something like NUMERIC which would convert from logical
to numeric. It would make things simpler to understand for beginners who
might not understand the meaning of +x_logical.

With that said, I realize it's really not a big deal. If I cared enough I
would wrap the +x_logical in my own NUMERIC function...but I don't. :)

Until later,

Jordan

"us" <us@neurol.unizh.ch> wrote in message
news:91bvsd$tr3$1@nnrp1.deja.com...
> you're correct: one should always mention the ver used (especially
> during such tedious transitional times 5.x -> 6.x).
> us
> sys: i586/w2k.sp1/r12
>
> In article <wkr93awmrv.fsf@math.uio.no>,
> jacklam@math.uio.no (Peter J. Acklam) wrote:
> > us <us@neurol.unizh.ch> writes:
> >
> > > help logical
> > > is very clear on how to get rid of the flag:
> > > +a_logical
> >
> > Yes, but this was changed from R11 to R12. The old R11 docs said
> > that the logical flag could be removed by adding a zero.
> >
> > Peter
> >
> > --
> > function f,f=fopen(which(mfilename));.......................Just
> > while ~feof(f),l=fgetl(f);fprintf(.......................another
> > '%s ',l(1+max(find(l=='.')):end)),........................Matlab
> > end,fclose(f);fprintf('\n')%..............................hacker
> >
>
>
> Sent via Deja.com
> http://www.deja.com/

Subject: How to convert a logical to double

From: jacklam@math.uio.no (Peter J. Acklam)

Date: 15 Dec, 2000 20:22:39

Message: 10 of 12

"Jordan Rosenthal" <jr@ece.gatech.edu> writes:

> Anyway, I guess my conclusion from all this is that, ideally,
> there should be a function called something like NUMERIC which
> would convert from logical to numeric.

The help text LOGICAL (5.3.0):

    LOGICAL Convert numeric values to logical.

but I find this somewhat misleading, since

    >> x = logical(1)

    x = 1

    >> isnumeric(x)

    ans = 1

Literallyl, the opposite of `logical' is `illogical', so here is
`illogical.m'... :-)

    function y = illogical(x)
    %ILLOGICAL Turn logical flag off.
    %
    % See also LOGICAL, ISLOGICAL.

    y = +x;

(This will not work for objects for which UPLUS is not defined,
though.)

> It would make things simpler to understand for beginners who
> might not understand the meaning of +x_logical.

But then again, it does seem a little silly to have a separate
function to do what can be done with one single character.

Peter

--
function f,f=fopen(which(mfilename));.......................Just
while ~feof(f),l=fgetl(f);fprintf(.......................another
'%s ',l(1+max(find(l=='.')):end)),........................Matlab
end,fclose(f);fprintf('\n')%..............................hacker

Subject: How to convert a logical to double

From: Jordan Rosenthal

Date: 15 Dec, 2000 16:45:31

Message: 11 of 12

Illogical.....I love it! :)

Jordan

"Peter J. Acklam" <jacklam@math.uio.no> wrote in message
news:wk8zphlcao.fsf@math.uio.no...
> "Jordan Rosenthal" <jr@ece.gatech.edu> writes:
>
> > Anyway, I guess my conclusion from all this is that, ideally,
> > there should be a function called something like NUMERIC which
> > would convert from logical to numeric.
>
> The help text LOGICAL (5.3.0):
>
> LOGICAL Convert numeric values to logical.
>
> but I find this somewhat misleading, since
>
> >> x = logical(1)
>
> x = 1
>
> >> isnumeric(x)
>
> ans = 1
>
> Literallyl, the opposite of `logical' is `illogical', so here is
> `illogical.m'... :-)
>
> function y = illogical(x)
> %ILLOGICAL Turn logical flag off.
> %
> % See also LOGICAL, ISLOGICAL.
>
> y = +x;
>
> (This will not work for objects for which UPLUS is not defined,
> though.)
>
> > It would make things simpler to understand for beginners who
> > might not understand the meaning of +x_logical.
>
> But then again, it does seem a little silly to have a separate
> function to do what can be done with one single character.
>
> Peter
>
> --
> function f,f=fopen(which(mfilename));.......................Just
> while ~feof(f),l=fgetl(f);fprintf(.......................another
> '%s ',l(1+max(find(l=='.')):end)),........................Matlab
> end,fclose(f);fprintf('\n')%..............................hacker

Subject: How to convert a logical to double

From: jacklam@math.uio.no (Peter J. Acklam)

Date: 15 Dec, 2000 23:23:06

Message: 12 of 12

"Jordan Rosenthal" <jr@ece.gatech.edu> writes:

> Illogical.....I love it! :)

I'm glad.. :-)

And `illogical array' is a lot shorter than `array with the
logical flag switched off'. But I have to admit that the
latter is a little more descriptive.

Peter

--
function f,f=fopen(which(mfilename));.......................Just
while ~feof(f),l=fgetl(f);fprintf(.......................another
'%s ',l(1+max(find(l=='.')):end)),........................Matlab
end,fclose(f);fprintf('\n')%..............................hacker

Tags for this Thread

No tags are associated with 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