Identify rows of a matrix, which contain 0's, where there are 1's in an array.

2 ビュー (過去 30 日間)
John
John 2023 年 12 月 16 日
コメント済み: the cyclist 2023 年 12 月 16 日
FSM = [0 1 0 1 1 1 0 0 0 0 0 0;
0 1 0 1 0 0 1 1 0 0 0 0;
0 1 0 1 0 0 0 0 1 1 0 0;
1 0 0 0 0 0 0 0 0 0 0 0;
1 1 1 1 0 0 0 0 0 0 0 1;
0 1 0 1 0 0 0 1 0 0 0 0;
0 1 0 1 0 0 0 0 0 1 0 0;
0 1 0 1 0 0 0 0 0 0 1 0];
O1 = [0 0 0 0 1 1 0 0 0 0 0 0];
O2 = [0 0 0 0 0 0 1 1 0 0 0 0];
O3 = [0 0 0 0 0 0 0 0 1 1 0 0];
O4 = [1 0 0 0 0 0 0 0 0 0 0 0];
O5 = [1 1 1 1 0 0 0 0 0 0 0 0];
O6 = [0 0 1 0 0 0 0 1 0 0 0 0];
O7 = [0 0 0 0 0 0 0 0 0 1 0 0];
O8 = [0 0 0 0 0 0 0 0 0 0 1 0];
I need to find the locations of the 1's in the O arrays, if there is any rows in the FSM matrix, where there are 0's in ALL of these places, that row returns a 0. However, if there is a 1 in any of these locations, that row returns a 1.
In the case of O1, the response would be [1 0 0 0 0 0 0 0], because there are 0's in the 5th AND 6th column of every row, other than the first.
I have several more arrays O2-O20, which contain various combinations of 0's and 1's

採用された回答

the cyclist
the cyclist 2023 年 12 月 16 日
I'm not quite certain this algorithm does what you want, since you only gave one example of the correct output, but I think so.
FSM = [0 1 0 1 1 1 0 0 0 0 0 0;
0 1 0 1 0 0 1 1 0 0 0 0;
0 1 0 1 0 0 0 0 1 1 0 0;
1 0 0 0 0 0 0 0 0 0 0 0;
1 1 1 1 0 0 0 0 0 0 0 1;
0 1 0 1 0 0 0 1 0 0 0 0;
0 1 0 1 0 0 0 0 0 1 0 0;
0 1 0 1 0 0 0 0 0 0 1 0];
O1 = [0 0 0 0 1 1 0 0 0 0 0 0];
% Result for O1
out1 = any(FSM(:,logical(O1)),2)'
out1 = 1×8 logical array
1 0 0 0 0 0 0 0
  2 件のコメント
John
John 2023 年 12 月 16 日
This is exactly what I was looking for.
Thank you!
the cyclist
the cyclist 2023 年 12 月 16 日
Please carefully consider @DGM's answer as well. It is generally a terrible idea to use dynamically named variables.
See this tutorial about the many reasons why.

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

その他の回答 (1 件)

DGM
DGM 2023 年 12 月 16 日
編集済み: DGM 2023 年 12 月 16 日
Putting indices in the variable names only makes everything worse.
FSM = [0 1 0 1 1 1 0 0 0 0 0 0;
0 1 0 1 0 0 1 1 0 0 0 0;
0 1 0 1 0 0 0 0 1 1 0 0;
1 0 0 0 0 0 0 0 0 0 0 0;
1 1 1 1 0 0 0 0 0 0 0 1;
0 1 0 1 0 0 0 1 0 0 0 0;
0 1 0 1 0 0 0 0 0 1 0 0;
0 1 0 1 0 0 0 0 0 0 1 0];
% don't create piles of index-named variables
allO = [0 0 0 0 1 1 0 0 0 0 0 0;
0 0 0 0 0 0 1 1 0 0 0 0;
0 0 0 0 0 0 0 0 1 1 0 0;
1 0 0 0 0 0 0 0 0 0 0 0;
1 1 1 1 0 0 0 0 0 0 0 0;
0 0 1 0 0 0 0 1 0 0 0 0;
0 0 0 0 0 0 0 0 0 1 0 0;
0 0 0 0 0 0 0 0 0 0 1 0];
% permute
allO = permute(allO,[3 2 1]);
% process
output = permute(any(FSM & allO,2),[3 1 2])
output = 8×8 logical array
1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1 1 1 0 1 0 0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1
Each row of the output array corresponds to the rows in allO. For example, output(1,:) is the example you gave for O1.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by