1 view (last 30 days)

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

Opportunities for recent engineering grads.

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

Start Hunting!
## 3 Comments

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/420620-how-can-i-find-the-elements-in-the-matrix-that-meet-my-conditions-and-then-form-the-matrix-within-th#comment_614073

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/420620-how-can-i-find-the-elements-in-the-matrix-that-meet-my-conditions-and-then-form-the-matrix-within-th#comment_614073

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/420620-how-can-i-find-the-elements-in-the-matrix-that-meet-my-conditions-and-then-form-the-matrix-within-th#comment_614080

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/420620-how-can-i-find-the-elements-in-the-matrix-that-meet-my-conditions-and-then-form-the-matrix-within-th#comment_614080

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/420620-how-can-i-find-the-elements-in-the-matrix-that-meet-my-conditions-and-then-form-the-matrix-within-th#comment_614107

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/420620-how-can-i-find-the-elements-in-the-matrix-that-meet-my-conditions-and-then-form-the-matrix-within-th#comment_614107

Sign in to comment.