how can I find the elements in the matrix that meet my conditions and then form the matrix within these conditions?

1 view (last 30 days)
YH on 25 Sep 2018
Edited: YH on 26 Sep 2018
Hallo everybody, I have a matrix of latitudes of earth locations from pool to pool obtained from a satellite pass. the matrix is 20x2573 double with many values set as NaN. I need only the latitudes of the Mediterranean Sea: 28°N to 46°N Latitude. Does anyone knows how I can limit my matrix to only the latitudes needed without changing the shape of it. that means the rows must stay 20 because they refer to 20 herz values and with NaN values too. Big thanks in advance!

Guillaume on 25 Sep 2018
Am I understanding correctly that the content of the matrix is the latitudes, e.g.
M = [28 20 46 50 NaN 10
15 NaN 32 42 71 NaN]
You say that the rows correspond to some sort of sampling, what do the columns correspond to?
If that is the way your latitudes are stored, then you cannot limit your matrix. What you can do is limit later processing to the applicable elements of your matrix. More details of the desired processing would be required.
YH on 25 Sep 2018
the rows are for the same latitude but processed at 20 Herz(20 values for the same latitude) the columns are the latitudes (regarding the satellite pass, I have 2573 locations with their latitudes). the interval I need is between 28 and 46 of latitude I attached the matrix as mat-form, maybe that will be more understandable.
Guillaume on 25 Sep 2018
Ok, but you need to explain better what limit the matrix actually mean.
It could mean that you want to keep columns 87 to 222, i.e any column that has values in the closed interval [24, 46] (or is it half-closed interval [24, 47) ?)
It could mean the same, but for the edge columns, any value out of the interval is replaced by NaN.
It could be that you want to keep columns whose median or mean is within your interval.
Something else altogether?

Stephan on 25 Sep 2018
Edited: Stephan on 25 Sep 2018
Hi,
here is an example:
% Make a random Matrix with 50x3 random integers between 1...100
A = randi(100,50,3);
% Use only the rows of A for B, where in column 1 of A the values are between 28 and 46
B = A(A(:,1)>=28 & A(:,1)<=46,:)
EDIT:
For your case this should work:
keep_col = sum(lat >= 28 & lat <= 46) >= 1;
lat2 = lat(:,keep_col);
You get a 20x136 Matrix lat2 which meets the condtions and all NaN values are kept.
Best regards
Stephan

Guillaume on 25 Sep 2018
Note:
keep_col = sum(lat >= 28 & lat <= 46) >= 1;
is probably better written as
keep_col = any(lat >= 28 & lat <= 46);
or, if the matrix can ever have just one row, for safety:
keep_col = any(lat >= 28 & lat <= 46, 1); %to make sure that the any/sum always operate across rows.
It's assuming that's what is meant by limit the matrix
Stephan on 25 Sep 2018
Thanks for the hint
YH on 26 Sep 2018
Thank you so much Stephan Jung and Guillaume for the help. it works out perfeclty for the postprocessing that I need to do!