Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert a logical to double

30 views
Skip to first unread message

Jordan Rosenthal

unread,
Dec 14, 2000, 12:15:25 PM12/14/00
to
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


Peter J. Acklam

unread,
Dec 14, 2000, 12:59:22 PM12/14/00
to
"Jordan Rosenthal" <j...@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

Jordan Rosenthal

unread,
Dec 14, 2000, 1:25:38 PM12/14/00
to
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" <jac...@math.uio.no> wrote in message
news:wkhf46o...@math.uio.no...

Peter J. Acklam

unread,
Dec 14, 2000, 1:50:27 PM12/14/00
to
"Jordan Rosenthal" <j...@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.

Jordan Rosenthal

unread,
Dec 14, 2000, 1:57:24 PM12/14/00
to
Fair enough....thanks.

Jordan

"Peter J. Acklam" <jac...@math.uio.no> wrote in message

news:wkvgsml...@math.uio.no...

us

unread,
Dec 14, 2000, 6:32:10 PM12/14/00
to
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>,


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

Peter J. Acklam

unread,
Dec 14, 2000, 7:28:36 PM12/14/00
to
us <u...@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.

us

unread,
Dec 14, 2000, 9:32:45 PM12/14/00
to
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 <wkr93aw...@math.uio.no>,

Jordan Rosenthal

unread,
Dec 15, 2000, 10:11:30 AM12/15/00
to
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" <u...@neurol.unizh.ch> wrote in message
news:91bvsd$tr3$1...@nnrp1.deja.com...

Peter J. Acklam

unread,
Dec 15, 2000, 2:22:39 PM12/15/00
to
"Jordan Rosenthal" <j...@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.

Jordan Rosenthal

unread,
Dec 15, 2000, 4:45:31 PM12/15/00
to
Illogical.....I love it! :)

Jordan

"Peter J. Acklam" <jac...@math.uio.no> wrote in message

news:wk8zphl...@math.uio.no...

Peter J. Acklam

unread,
Dec 15, 2000, 5:23:06 PM12/15/00
to
"Jordan Rosenthal" <j...@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.

Parshuram M. Kamble

unread,
Aug 26, 2017, 5:25:20 AM8/26/17
to
"Jordan Rosenthal" <j...@ece.gatech.edu> wrote in message <91av64$hbd$1...@news-int.gatech.edu>...

Parshuram M. Kamble

unread,
Aug 26, 2017, 5:34:17 AM8/26/17
to
"Jordan Rosenthal" <j...@ece.gatech.edu> wrote in message <91av64$hbd$1...@news-int.gatech.edu>...
Clear all;
close all;
I=imread('imageTest1.bmp');
[r,c]=size(I)
g=[];
g=double(I);

dpb

unread,
Dec 16, 2017, 9:51:37 AM12/16/17
to
On 8/26/2017 4:34 AM, Parshuram M. Kamble wrote:
> "Jordan Rosenthal" <j...@ece.gatech.edu> wrote in message
> <91av64$hbd$1...@news-int.gatech.edu>...
>> 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

Of course double(a_logical) works; it's just that a_double ~= [ 1 2 3 ]
if that is the intent. Once the operation logical( [ 1 2 3 ] ) has been
completed, there's no recovering the numeric values of 2, 3; what the
specific values were is gone entirely; only the result of whether the
values were/were not zero is retained.

The lesson to be learned is that if one is going to need the numeric
values of the initial vector again, don't destroy them.

--

0 new messages