Is there a faster alternative for Cell array?
Afficher commentaires plus anciens
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 commentaire
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
Réponse acceptée
Plus de réponses (1)
Idossou Marius Adom
le 28 Avr 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)];
Catégories
En savoir plus sur Performance and Memory dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!