If loop issue help

8 ビュー (過去 30 日間)
Ricardo jose
Ricardo jose 2021 年 3 月 23 日
回答済み: Star Strider 2021 年 3 月 23 日
Hello everyone, Im trying to evaluate "Acceleration_Value" and categoryze them as 1, 2 or 3 to depending on thei're value. Im having the issue that ds_mapping an array, stays completly full of 0, what am i doing wrong?
Acceleration_Value= abs(Acceleration); % Obtaining absolute values for acceleration/deceleration
ds_mapping=zeros(size(Acceleration)); % Creating variable to store evaluated values
i= ds ;
if 0.7 <= i & i>= 2.79 %#ok<AND2>
ds_mapping= 1;
elseif 2.79 < i & i >= 3.63 %#ok<AND2>
ds_mapping= 2;
elseif 3.63 < i & i >= 6.5 %#ok<AND2>
ds_mapping= 3;
end
  2 件のコメント
Jan
Jan 2021 年 3 月 23 日
There are no "if loops" in any programming language I know.
Ricardo jose
Ricardo jose 2021 年 3 月 23 日
the "if" condition then. By looking code can you understand what im struggling with?

サインインしてコメントする。

回答 (2 件)

Star Strider
Star Strider 2021 年 3 月 23 日
An easier way to code that is to use an anonymous function:
ds_mapping = @(i) ((0.7 <= i) & (i>= 2.79 )).*1 + ((2.79 < i) & (i >= 3.63 )).*2 + ((3.63 < i) & (i >= 6.5 )).*3;
ds = linspace(0, 7);
figure
plot(ds, ds_mapping(ds))
grid
See the documentation on Anonymous Functions for details.

Jan
Jan 2021 年 3 月 23 日
編集済み: Jan 2021 年 3 月 23 日
The if command is not a loop. It requires a scalar condition. You provide vectors, so Matlab inserts an all() automatically.
The "%#ok<AND2>" seems to show, that you have ignored another hint of the editor already: The case 0.7 <= i & i>= 2.79 canbe simplified to i >= 2.79. The same happens for the other branches. It is a good idea not to disable the warnings, but to fix the reason of the warnings.
Do you mean:
if 0.7 <= i & i <= 2.79
% ^ not > ?! Same in the other cases
end
Solution: Either create a loop or use logical indexing. With the data you have posted, both will not run:
ds_mapping=zeros(size(Acceleration)); % Creating variable to store evaluated values
i= ds ;
What is ds ?
  2 件のコメント
Ricardo jose
Ricardo jose 2021 年 3 月 23 日
ds is a "diff(time)"
Jan
Jan 2021 年 3 月 23 日
You did not mention a variable called "time" yet...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by