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.

 采纳的回答

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 个评论

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.
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 Exchange 中查找有关 Cell Arrays 的更多信息

产品

版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by