How do I write an If() AND() function in MATLAB?

2 ビュー (過去 30 日間)
Julie
Julie 2016 年 1 月 27 日
コメント済み: Star Strider 2016 年 1 月 27 日
Hi,
I was wondering what the best way to write an IF() AND() function is. For example if I have the following information, I want to select the values in row 3 associated with the values that have 11 in column1 and have a 1 in column2. How do I write the code to do this?
column1 column2 column 3 column 4
11 1 6 5
11 1 8 2
12 1 7 0
21 1 3 8
21 1 2 9

採用された回答

Star Strider
Star Strider 2016 年 1 月 27 日
This works:
M = [11 1 6 5
11 1 8 2
12 1 7 0
21 1 3 8
21 1 2 9];
Out = M((M(:,1) == 11) & (M(:,2) == 1),3)
Out =
6
8
  2 件のコメント
Julie
Julie 2016 年 1 月 27 日
THANKS! Would this also work for a variable? So for example if the previous matrix is an excel sheet I loaded to matlab and then said x = colum1 + 5. Then I wanted to select the values in x that were associated with 11 in column1 and had a 1 in column2?
Star Strider
Star Strider 2016 年 1 月 27 日
My pleasure!
Yes it would. In fact, here ‘M’ is a variable, so if I understand you correctly, you would just substitute ‘x’ for ‘M’ to make my code compatible with your variables.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by