Return logical for rows of non-zero numbers for specified columns (similar to "all" function)

1 回表示 (過去 30 日間)
Hi. I need to calculate a logical from a 1x3 matrix (although the number of columns is irrelevant) that tells me if non-zero numbers are in specified columns.
For example, if I have the following matrix callled mat:
4 16 2
6 7 7
1 4 8
0 5 19
3 0 5
9 0 16
x = all(mat,2)
x here would return a logical array where each row equaling "1" will have ALL non-zero numbers. In mat, this would only be the first 3 rows, and thus x = [1 1 1 0 0 0];
My question is whether there is an easy line of code or existing function that would allow me to specifiy which columns I would like to be non-zero in order for the logical to spit out a ero. For example, if I wanted all rows where columns 1 and 3 are non-zero, the logical that is returned should be something like this: x = [0 0 0 0 1 1] since only the last two rows fit that criteria.
Or if I wanted all rows where columns 2 and 3 were non-zero, it should return x = [0 0 0 1 0 0]
Thanks in advance!
  1 件のコメント
Matt J
Matt J 2021 年 8 月 10 日
For example, if I wanted all rows where columns 1 and 3 are non-zero, the logical that is returned should be something like this: x = [0 0 0 0 1 1] since only the last two rows fit that criteria.
Why don't the first 3 rows fit the criteria? Shouldn't the result be x = [1,1,1 0 1 1]

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

採用された回答

Matt J
Matt J 2021 年 8 月 10 日
編集済み: Matt J 2021 年 8 月 10 日
For example, if I wanted all rows where columns 1 and 3 are non-zero
This would be,
all(mat(:,[1,3]),2)
  3 件のコメント
BobbyRoberts
BobbyRoberts 2021 年 8 月 10 日
Like so:
all(mat(:,[1,3]),2) - all(mat(:,2),2)
Matt J
Matt J 2021 年 8 月 10 日
Better to do,
all(mat(:,[1,3]),2) & ~all(mat,2)

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by