I want to convert a logical array to any type of nuemeric array.
But I do not know how.
Any help is appreciated
Amal
A = rand(5) % a double array
LA = A < .5 % a logical array
DLA = double(LA) % logical array turned into a double array
James Tursa
a hint:
help double;
us
Another idea:
help cast
Jan
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
> 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
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
> 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
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
> 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