Using Conditions from One Array To Select Data from Another

I have an array that stores generated hurricane data for hundreds of years with column 1 as the year, column 2 as the latitude, and column 3 as the longitude. I have another array that has a specific selection of years that I want to pull the data from e.g. 857, 865, 890, etc. How do I set up the code to do that? I have tried quite a few different ways and I am always getting the "Arrays have incompatible sizes for this operation." error.
idx=find(comyearlatlong==year_weak_elnino)
I have tried looking into the documentation but I'm just not understanding it well enough to brute force it. I've attached the two files I'm using.

 採用された回答

Voss
Voss 2022 年 7 月 13 日

0 投票

load comyearlatlong.mat
load year_weak_elnino.mat
whos *year*
Name Size Bytes Class Attributes comyearlatlong 115600x3 2774400 double year_weak_elnino 149x1 1192 double
[ism,idx] = ismember(comyearlatlong(:,1),year_weak_elnino);
whos i*
Name Size Bytes Class Attributes idx 115600x1 924800 double ism 115600x1 115600 logical
ism is a 115600x1 logical vector saying whether each year in comyearlatlong(:,1) is in year_weak_elnino.
idx is a 115600x1 vector of indices - the location where each element of comyearlatlong(:,1) appears in year_weak_elnino, or 0 if that element of comyearlatlong(:,1) does not appear in year_weak_elnino.
Reference: ismember

2 件のコメント

Andrew
Andrew 2022 年 7 月 13 日
編集済み: Andrew 2022 年 7 月 13 日
So now that idx indicates yes/no if the years in comyearlatlong are in year_weak_elnino, how do you then pull those years along with the lat/long coordinates into a separate array?
Sorry for all the questions, I'm pretty fresh to MATLAB and am still trying to get the hang of a lot of the basics.
Voss
Voss 2022 年 7 月 13 日
No problem. Here's how you do it:
% ism is the yes/no (you may not actually need idx)
[ism,idx] = ismember(comyearlatlong(:,1),year_weak_elnino);
% get the year/lat/long data for el-nino years:
elnino_data = comyearlatlong(ism,:)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

製品

リリース

R2022a

質問済み:

2022 年 7 月 13 日

コメント済み:

2022 年 7 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by