Help on If loop
显示 更早的评论
Hello, Im trying to evaluate and separete by categories a certain Variable (Acceleration) and dividing it by levels (0,1,2,3) depending on its value to therefor do a table with them, however my code isnt really working, only obtain 1 value, which 0, when im expecting to get a 621x1 array. Leve here my code, if anyone can help i would appreciate.
Thank you.
Acceleration_Value= abs(Acceleration); % Obtaining absolute values for acceleration/deceleration
ds_mapping=[];
if 0.7 <= Acceleration_Value >= 2.79
ds_mapping= 'Econimic (1)';
elseif 2.79 < Acceleration_Value >= 3.63
ds_mapping= 'Normal (2)';
elseif 3.63 < Acceleration_Value >= 6.5
ds_mapping= 'Agressive (3)';
else
ds_mapping= 0
end
Table1= table(Acceleration_Value, ds_mapping)
回答(1 个)
Walter Roberson
2021-3-8
In MATLAB,
if 0.7 <= Acceleration_Value >= 2.79
is considered to be
if all((0.7 <= Acceleration_Value) >= 2.79)
The first part, (0.7 <= Acceleration_Value) returns 1 (true) for each element of Acceleration_Value that is greater than or equal to 0.7, and 0 for each element that is not. Then the 0 or 1 is compared to 2.79... and of course that will never be true.
Your use of table() suggests to me that your Acceleration is a vector rather than a scalar.
I would suggest you consider using discretize() with categorical
(and remember that table want column vectors not row vectors.)
类别
在 帮助中心 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!