A couple of possibilities:
1. x = repmat(char(0),10,4);
2. x(10,4) = char(0);
I prefer #1 because it will always work whereas #2 requires that there
is no x variable already in existence. You can precede it with clear x,
but then it's even messier.
--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
Doug Schwarz <s...@sig.for.address.edu> wrote in message <see-0D97FE.2...@news.frontiernet.net>...
> x = char(zeros(1,100));
> Elapsed time is 0.000033 seconds.
> y = repmat(char(0),1,100);
> Elapsed time is 0.000263 seconds.
> z = char(zeros(1,100,'uint8'));
> Elapsed time is 0.001665 seconds.
Other timings (Matlab 2009a):
tic; for i=1:1000,
x = repmat(char(0), 1, 100);
clear('x'); end; toc;
>> 0.051 sec
tic; for i=1:1000,
x = char(zeros(1, 100));
clear('x'); end; toc;
>> 0.021 sec
tic; for i=1:1000,
x = char(zeros(1, 100, 'uint8'));
clear('x'); end; toc;
>> 0.017 sec
tic; for i=1:1000,
b = char(0);
x = b(ones(1, 100));
clear('x'); end; toc;
>> 0.025 sec
Clearing the created variable in the loop makes the problem more realistic. Otherwise Matlab's smart JIT seems to understand more or less, that the repeated work is not necessary.
My conclusion: For real world problems, all methods will be sufficiently fast.
Kind regards, Jan
uninit(10,4,'char')
You can find the UNINIT utility here:
James Tursa
> You can find the UNINIT utility here:
>
> http://www.mathworks.com/matlabcentral/fileexchange/31362-uninit-create-an-uninitialized-variable-like-zeros-but-faster
>
I never feel a need of optimizing my codes at such level of detail, but surely such function should be part of Matlab.
Bruno