Is there a faster alternative for Cell array?

16 次查看(过去 30 天)
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 个评论
dpb
dpb 2020-4-28
编辑:dpb 2020-4-28
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

请先登录,再进行评论。

采纳的回答

dpb
dpb 2020-4-28
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].';

更多回答(1 个)

Idossou Marius Adom
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)];

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by