フィルターのクリア

Is there a faster alternative for Cell array?

16 ビュー (過去 30 日間)
AKSHAY PUJARI
AKSHAY PUJARI 2020 年 4 月 28 日
回答済み: dpb 2020 年 4 月 28 日
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
Idossou Marius Adom 2020 年 4 月 28 日
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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by