Is there a faster alternative for Cell array?

조회 수: 14 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by