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

1 次查看(过去 30 天)
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

更多回答(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 CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by