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

How to convert a logical array to a nuemeric array?

0 views
Skip to first unread message

almal...@hotmail.com

unread,
Jan 3, 2010, 2:02:31 AM1/3/10
to
Hi everybody,

I want to convert a logical array to any type of nuemeric array.
But I do not know how.
Any help is appreciated

Amal

James Tursa

unread,
Jan 3, 2010, 2:40:06 AM1/3/10
to
almal...@hotmail.com wrote in message <d6a362f3-cbc8-4d09...@m3g2000yqf.googlegroups.com>...

A = rand(5) % a double array
LA = A < .5 % a logical array
DLA = double(LA) % logical array turned into a double array

James Tursa

us

unread,
Jan 3, 2010, 9:10:04 AM1/3/10
to

a hint:

help double;

us

Jan Simon

unread,
Jan 3, 2010, 10:00:06 AM1/3/10
to
Dear Amal!

Another idea:
help cast
Jan

us

unread,
Jan 3, 2010, 10:29:03 AM1/3/10
to
"Jan Simon" <matlab.T...@nMINUSsimon.de> wrote in message <hhqbdm$t58$1...@fred.mathworks.com>...

well...

v=false(1,100000);
tic;
for i=1:1000
vd=double(v);
end
t1=toc;
tic;
for i=1:1000
vd=cast(v,'double');
end
t2=toc;
% wintel sys: i2c/2*2.6ghz/2gb/winxp.sp3.32/r2009b
disp([t1;t2]);
%{
0.20388 % <- DOUBLE
0.73422 % <- CAST
%}

...just a thought, as usual...
us

Jan Simon

unread,
Jan 3, 2010, 11:04:03 AM1/3/10
to
Dear us!

> 0.20388 % <- DOUBLE
> 0.73422 % <- CAST

> ...just a thought, as usual...

Nice, thank you, us! I didn't expect noticable differences in speed, therefore I didn't test it... The old human problem.

What about this, if the type is determined dynamically:
TypeName = 'double'
casted = feval(TypeName, data);
or TypeFcn = @double
casted = feval(TypeFcn, data);

Jan

us

unread,
Jan 3, 2010, 11:17:04 AM1/3/10
to
"Jan Simon" <matlab.T...@nMINUSsimon.de> wrote in message <hhqf5j$jkh$1...@fred.mathworks.com>...

timing for both FEVALs approx the same as for CAST, ie, on the same wintel sys

0.73422 % <- CAST
0.75392 % <- FEVAL(@double,...)
0.75349 % <- FEVAL('double',...)

the reason may(!) be:
- DOUBLE just sets some internal flag(s) and does 'some smart' realloc...
- all others run through/check/copy the whole array...

best
urs

Jan Simon

unread,
Jan 3, 2010, 11:29:04 AM1/3/10
to
Dear us!

> timing for both FEVALs approx the same as for CAST, ie, on the same wintel sys
> 0.73422 % <- CAST
> 0.75392 % <- FEVAL(@double,...)
> 0.75349 % <- FEVAL('double',...)
>
> the reason may(!) be:
> - DOUBLE just sets some internal flag(s) and does 'some smart' realloc...
> - all others run through/check/copy the whole array...

In Matlab 5.3 LOGICALs have been DOUBLEs, so the conversion set a single flag only. But since Matlab 6.0 LOGICALs are an individual type, which need 1 byte per element. Therefore even the conversion with DOUBLE(Array) must need the same copying.

I'm surprised, that FEVAL(@double) and FEVAL('double') need the same time! I thought that the function handle is much faster. But then I remembered, that "@double" has to search the referenced function for each call also. So the faster method would be:
fcn = @double;
for i = 1:10000, casted = feval(fcn, data); end

It would be nice, if we could ask the developper of CAST where the time is lost.
Kind regards, Jan

Steven Lord

unread,
Jan 3, 2010, 10:21:25 PM1/3/10
to

"Jan Simon" <matlab.T...@nMINUSsimon.de> wrote in message
news:hhqgkg$jkq$1...@fred.mathworks.com...

> Dear us!
>
>> timing for both FEVALs approx the same as for CAST, ie, on the same
>> wintel sys
>> 0.73422 % <- CAST
>> 0.75392 % <- FEVAL(@double,...)
>> 0.75349 % <- FEVAL('double',...)
>>
>> the reason may(!) be:
>> - DOUBLE just sets some internal flag(s) and does 'some smart' realloc...
>> - all others run through/check/copy the whole array...
>
> In Matlab 5.3 LOGICALs have been DOUBLEs, so the conversion set a single
> flag only. But since Matlab 6.0 LOGICALs are an individual type,

The LOGICAL class was introduced in MATLAB 6.5 (release R13), not MATLAB 6.0
(release R12).

> which need 1 byte per element. Therefore even the conversion with
> DOUBLE(Array) must need the same copying.
>
> I'm surprised, that FEVAL(@double) and FEVAL('double') need the same time!
> I thought that the function handle is much faster. But then I remembered,
> that "@double" has to search the referenced function for each call also.
> So the faster method would be:
> fcn = @double;
> for i = 1:10000, casted = feval(fcn, data); end

If you're using a relatively recent version of MATLAB, you don't need to use
FEVAL.

data = false(1, 100000);
fcn = @double;
for k = 1:10000, casted = fcn(data); end

> It would be nice, if we could ask the developper of CAST where the time is
> lost.

I'm not sure I understand what you're asking. Are you asking about the
difference between the CAST and FEVAL solutions? I'm believe from his posts
that us ran those timings in a FOR loop running a thousand times, which
means the average difference between one CAST call and one FEVAL call is
2e-5 -- I wouldn't worry about that unless I was calling CAST or FEVAL
hundreds of thousands or millions of times. If that was the case, I'd see
if I could make a smaller number of vectorized calls.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


Jan Simon

unread,
Jan 4, 2010, 3:14:04 AM1/4/10
to
Dear Steven!

> I'm not sure I understand what you're asking. Are you asking about the
> difference between the CAST and FEVAL solutions?

No. I think, I understand the similar speed of CAST and FEVAL. I've just omitted the loops in my example for simplicity - vectorization is not the problem here.

Thanks for rectification of the Matlab version, which introduces LOGICALs as type!

I'm just wondering, why the direct call of DOUBLE is faster, as us has measured:
v = false(1,100000);
tic; for i = 1:1000, vd = double(v); end, t1=toc;
tic; for i = 1:1000, vd = cast(v,'double'); end, t2=toc;

% wintel sys: i2c/2*2.6ghz/2gb/winxp.sp3.32/r2009b


0.20388 % <- DOUBLE
0.73422 % <- CAST

And similar times as CAST for FEVAL(@double, v) and FEVAL('double', v).
I'd expect, that CAST(v, 'double') and DOUBLE(v) take equivalent times, because the overhead for parsing the string 'double' cannot be too expensive in the CAST command.

Kind regards, Jan

0 new messages