Index satisfying multiple conditions

58 ビュー (過去 30 日間)
NMans
NMans 2018 年 3 月 28 日
コメント済み: NMans 2018 年 3 月 28 日
Hi, I have a matrix with 4 columns of Year, Month, Hour, Speed. I would like to extract the data from just month 1, month 11 and month 12. My code is as below:
Wind_All = [year month day speed];
loc = find(Wind_All(:,2)==1 & Wind_All(:,2)>=11);
YEAR = [year(loc(1):max(loc))];
MONTH = [month(loc(1):max(loc))];
DAY = [day(loc(1):max(loc))];
SPEED = [speed(loc(1):max(loc))];
However, it's giving me an error message:
Index exceeds matrix dimensions. Error in Speed_Bin6 (line 34); YEAR = [year(loc(1):max(loc))];
Can someone tell me what I did wrong there? It works if it's:
loc = find(Wind_All(:,1)==2015 & Wind_All(:,2)>=11); &&indexing year 2015 and month 11 and 12.
Thank you!

採用された回答

Guillaume
Guillaume 2018 年 3 月 28 日
I do not really understand what you are trying to do with the loc(1):max(loc) bit.
As Elias says, you want | instead of &. But note that you also did not need the find:
Wind_All = [year month day speed];
loc = Wind_All(:, 2) == 1 | Wind_All(:, 2) >= 11; %No need for find. Use logical indexing instead
filtered = Wind_All(loc, :)
And personally, I would use ismember instead of your logical expression:
Wind_All = [year month day speed];
loc = ismember(Wind_All(:, 2), [1, 11, 12]);
filtered = Wind_All(loc, :)
  1 件のコメント
NMans
NMans 2018 年 3 月 28 日
Thanks these also work!!

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

その他の回答 (1 件)

Elias Gule
Elias Gule 2018 年 3 月 28 日
Just replace the '&' with '|'. Because you want the value in column 2 where the month is 1 or where the month is greater or equal to 11.
  1 件のコメント
NMans
NMans 2018 年 3 月 28 日
thanks this works!

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

カテゴリ

Help Center および File ExchangeNaNs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by