Extracting values from one column by specifying range for two other columns

Hi, I have a 556*4 matrix. I want to extract/display values from column 1 when -32.5<=x<=40 and 54.5<=y<=62.5 where x and y are 3rd and 4th column values of the same matrix. How can I do this? Other than using for and if loop, is there any other way?
If this is very basic question, then please tell which tutorial or manual should I refer to?

 採用された回答

David Young
David Young 2014 年 2 月 26 日
Using A to refer to your matrix, try
x = A(:, 3);
y = A(:, 4);
ok = -32.5 <= x & x <= 40 & 54.5 <= y & y <= 62.5;
results = A(ok, 1);
For documentation, give the command
doc
and in the help window look at MATLAB > Language Fundamentals > Matrices and Arrays

3 件のコメント

Venkatesh M Deshpande
Venkatesh M Deshpande 2014 年 2 月 26 日
thanks. this really works well. However, I am not able to understand how "ok" is working. In workspace, ok is seen as logical. What is the data type of ok?
David Young
David Young 2014 年 2 月 27 日
Yes, ok is logical. It has a true value in every row where the inequalities are all satisfied, and a false value in the other rows. When used as an index, only the rows with true values in ok are selected.
Venkatesh M Deshpande
Venkatesh M Deshpande 2014 年 2 月 27 日
ok, thank you.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by