Borrar filtros
Borrar filtros

Is there a faster alternative for Cell array?

16 visualizaciones (últimos 30 días)
AKSHAY PUJARI
AKSHAY PUJARI el 28 de Abr. de 2020
Respondida: dpb el 28 de Abr. de 2020
I have the following for loop that classifies the speed and temperature values into classes and stores the value in cell arrays.
My aim is to reduce the time of calculation.Cell format appears to be relatively slow.
speed=[23 337 510 0.0104 450];
Temperature=[35 45 55 65 75];
for t=1:size(speed,2)
Temp_class=round( (Temperature(t)-35)/5+1 );%Temp_classs values are 1,2,3etc
if abs(speed(t))<1
Spd_class=1;
elseif (1<=speed(t)) && (speed(t)<(500)
Spd_class=2;
else
Spd_class=round( (speed(t)-0)/250 +2 );%Spd_class values are 1,2,3 etc
end
Result{Temp_class,Spd_class}=[Result{Temp_class,Spd_class};[t,Temperature(t),speed(t)]];
end
The main requirement is to identify the speed and temperature data points that belong to the same class.
For Ex: all the speed,Temperature data points with speed class 5 and temperature class 2 will be stored in cell array at {2,5}.
I would like to find a alternative faster way of storing the data.
Thanks in advance
  1 comentario
dpb
dpb el 28 de Abr. de 2020
Editada: dpb el 28 de Abr. de 2020
Result{Temp_class,Spd_class}=[Result{Temp_class,Spd_class};[t,Temperature(t),speed(t)]];
is the bottleneck, you're dynamically reallocating the cell content every pass.
There's also an unbalanced parenthesis on elseif (1<speed(t)) && (speed(t)<(500)
elseif (1<speed(t)) && (speed(t)<500)
You can't return a Spd_class value of 3 -- <500 --> 2 by the second clause but
round(500/250+2) ==> 4, not 3 to be continuous

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 28 de Abr. de 2020
Just store the class data with the position/time variable in 2D array. Use grouping variables to process by class either singly or jointly.
if abs(speed)<1
sclass=1;
elseif speed>=1 & speed<500
sclass=2;
else
sclass=fix(speed/250)+1;
end
tclass=fix((Temperature-35)/)5+1;
TSclasses=[1:numel(speed);sclass;tclass].';

Más respuestas (1)

Idossou Marius Adom
Idossou Marius Adom el 28 de Abr. de 2020
You may consider specifying the size of the cell array
Result = cell(m,n) % m rows and n columns
and then index the array in the last line of your loop instead of reallocating the whole cell array.
Resul{Temp_class,Spd_class} = [t,Temperature(t),speed(t)];

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by