フィルターのクリア

If Else Statement for the rainfall data

1 回表示 (過去 30 日間)
Saleem Sarwar
Saleem Sarwar 2015 年 10 月 1 日
回答済み: Andrei Bobrov 2015 年 10 月 1 日
I have a time series of daily rainfall data from 1950 to 2013. I want to implement following conditions to the data if rainfall is less than 0.5 then 1 , if rainfall is between 0.5 to 1.1 then 2 otherwise 3. How to write the code for this analysis?

採用された回答

Walter Roberson
Walter Roberson 2015 年 10 月 1 日
function rlev = rain_level(rainfall)
rlev = ones(size(rainfall));
idx = rainfall > 0.5 & rainfall < 1.1;
rlev(idx) = 2;
idx = rainfall >= 1.1 | rainfall == 0.5;
rlev(idx) = 3;
end
The "rainfall == 0.5" is necessary because 0.5 exactly is not less than 0.5 and is not between 0.5 and 1.1 either; you indicated 3 "otherwise" so the 3 has to apply to all ranges not listed, including the 0.5 exactly.
1.1 exactly cannot happen in binary floating point (1/10th is an infinite repeating number in binary) so the question of whether 1.1 exactly belongs in level 2 or 3 becomes irrelevant, and we might as well code >= on the 1.1 for clarity.

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2015 年 10 月 1 日
[~,~,ii]=histcounts(data_of_daily_rainfall,[-inf,.5,1.1,inf]);
y = 1:3;
out = y(ii);

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by