How can I create a new vector using the if function?

1 回表示 (過去 30 日間)
G
G 2016 年 6 月 4 日
コメント済み: G 2016 年 6 月 4 日
Hi everyone,
I have the following problem:
I have a vector (V) that range from 0 to 60 and I would like to group these data as following:
* If the value of (V) is between or equal to 0 and 5 , give me 5
* If the value of (V) is between or equal to 6 and 10 , give me 10
...
* If the value of (V) is between or equal to 56 and 60 , give me 60
The vector V contains 60 data, and the new vector should contain also 60 data
Thank you in advance

採用された回答

John D'Errico
John D'Errico 2016 年 6 月 4 日
So you want to round up to the nearest multiple of 5.
Divide by 5, use ceil, then multiply by 5. The only catch is that for 0, this will result in 0. So catch that possibility too using max.
Vhat = 5*ceil(max(1,V/5));
Learn to use tools that apply to entire arrays and vectors in MATLAB.
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2016 年 6 月 4 日
+1
G
G 2016 年 6 月 4 日
Thank you John ! It works!

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

その他の回答 (1 件)

the cyclist
the cyclist 2016 年 6 月 4 日
ceil(V/5) * 5
  2 件のコメント
John D'Errico
John D'Errico 2016 年 6 月 4 日
This misses the 0 case.
the cyclist
the cyclist 2016 年 6 月 4 日
Yep

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by