Preallocating str for speed

I have this code. I want it to run faster. And I keep getting the warning to preallocate str for speed.
clc
str=[];
for T='956754674275'
if T=='0'
str = [str sprintf('A')];
elseif T=='1'
str = [str sprintf('B')];
elseif T=='2'
str = [str sprintf('C')];
elseif T=='3'
str = [str sprintf('D')];
elseif T=='4'
str = [str sprintf('E')];
elseif T=='5'
str = [str sprintf('F')];
elseif T=='6'
str = [str sprintf('G')];
elseif T=='7'
str = [str sprintf('H')];
elseif T=='8'
str = [str sprintf('I')];
elseif T=='9'
str = [str sprintf('J')];
end
end
a=str
Please how do I preallocate str for speed. Please help.

5 comentarios

Wan Ji
Wan Ji el 11 de Ag. de 2021
just do
a = '956754674275';
str = a;
Then
ct = 0;
for T='956754674275'
ct = ct+1;
if T=='0'
str(ct) = 'A'; % following you just do in this form
elseif T=='1'
str = [str sprintf('B')];
elseif T=='2'
str = [str sprintf('C')];
elseif T=='3'
str = [str sprintf('D')];
elseif T=='4'
str = [str sprintf('E')];
elseif T=='5'
str = [str sprintf('F')];
elseif T=='6'
str = [str sprintf('G')];
elseif T=='7'
str = [str sprintf('H')];
elseif T=='8'
str = [str sprintf('I')];
elseif T=='9'
str = [str sprintf('J')];
end
end
Ernest Adamtey
Ernest Adamtey el 11 de Ag. de 2021
Please it gives an output
'956754674275JFGHFEGHECHF'
instead of 'JFGHFEGHECHF'
Ernest Adamtey
Ernest Adamtey el 11 de Ag. de 2021
Thank you Wan Ji. how do I accept this comment as an answer.
Stephen23
Stephen23 el 11 de Ag. de 2021
Editada: Stephen23 el 11 de Ag. de 2021
"...how do I accept this comment as an answer."
Or take look at dpb's much better approach.
(assuming that you want neat, simple, very efficient code using basic character code operations)
Wan Ji
Wan Ji el 12 de Ag. de 2021
@Stephen Cobeldick@Ernest Adamtey, Yes, @dpb 's answer is pretty efficient and faster!

Iniciar sesión para comentar.

Respuestas (3)

Henry Barth
Henry Barth el 11 de Ag. de 2021

0 votos

correspondingChars = 'ABCDEFGHIJ';
str = correspondingChars(arrayfun(@str2double,T,'UniformOutput',true));
dpb
dpb el 11 de Ag. de 2021

0 votos

No loop needed here -- using relationship to ASCII coding and that MATLAB char() strings are just arrays, write
T='956754674275';
% the engine
C='A'-'0'; % difference offset from '0' to 'A' in code--MATLAB returns 17
str=char(T+C); % convert to char() from double of input + offset
As long as the substitution pattern is ordered sequentially, the above works no matter what the initial coding point is simply by changing the target reference in computing the offset constant, C. If it could be a variable, then create a lookup table and index into it instead.
Above produces--
>> T='956754674275';
>> C='A'-'0'
C =
17
>> char(T+C)
ans =
'JFGHFEGHECHF'
>> all(ans==str)
ans =
logical
1
>>
Wan Ji
Wan Ji el 12 de Ag. de 2021

0 votos

According to @dpb's answer, here I provide a more general answer!
T='956754674275';
encode = 'ABCDEFGHIJ';
str = encode(double(T)-double('0')+1)
then
str =
'JFGHFEGHECHF'

1 comentario

dpb
dpb el 12 de Ag. de 2021
Yes, that's the lookup table version mentioned.
NB: that the above can be more succinctly written as
str=encode(T-'0'+1);
or
str=encode(T-'/');

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Ag. de 2021

Comentada:

dpb
el 12 de Ag. de 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by