How to Filter Array Based on Values in a Different Array?

조회 수: 39 (최근 30일)
Kelsey Ward
Kelsey Ward 2022년 7월 19일
답변: Voss 2022년 7월 19일
I have 3 arrays of the same dimensions, and the same indices in these arrays correspond to the same individuals. One array gives the geographic location in latitude of each individual where each row is an individual and each column is a time step and then it gives the latitude at each time step. The 2nd array is the same thing but with longitude. My final array works similarly to give the status of each individual (alive, dead, etc), with the rows representing the same individuals and the columns representing the same time steps and then a numeric value at each index representing the status (-3, -2, -1, 0, or 1).
I want to pull out the locations of certain individuals with a particular status, so I need to pull out all the x and y coordinates from my other 2 arrays based on what number is in the corresponding index of the status array. So for example make new arrays that contain all the x and y coordinates of all alive individuals with status 1, or even better one new array that provides the x and y together for each individual. I have no idea how to even begin this if anyone could point me in the right direction.

채택된 답변

Voss
Voss 2022년 7월 19일
% some random lat/lon/stat matrices
lat = randi([-90 90],5,10)
lat = 5×10
-7 -28 -33 15 -52 -23 24 5 0 68 87 0 5 -11 -10 49 47 -65 1 -30 62 -35 -71 71 -8 -54 -84 15 86 57 66 32 -13 -31 -10 67 -74 -66 33 -90 52 -12 63 -76 60 -36 -18 -71 54 74
lon = randi([-180 180],5,10)
lon = 5×10
173 110 129 -37 152 -132 140 -94 116 -159 -88 168 -179 -10 -125 -5 13 -2 -101 -147 -13 16 18 -136 97 -24 -96 -125 156 125 116 3 134 78 179 -110 108 169 -152 -68 -142 -131 170 173 -64 -99 -161 34 -169 -117
stat = randi([-3 1],5,10)
stat = 5×10
-3 -3 -2 -1 -1 -1 1 -1 0 1 -2 1 -1 -2 0 1 0 -2 -2 1 -1 0 0 1 -1 1 -3 -1 -3 1 -2 -3 -3 1 1 0 1 -2 -1 -1 0 -3 -2 1 -3 -2 -2 0 -3 0
% get the lat and lon where stat == -2
idx = stat == -2;
lat_lon = [lat(idx) lon(idx)]
lat_lon = 10×2
87 -88 66 116 -33 129 63 170 -11 -10 -36 -99 -18 -161 -65 -2 -66 169 1 -101

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by