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
0 个评论
采纳的回答
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)'
2 个评论
the cyclist
2023-12-16
Please carefully consider @DGM's answer as well. It is generally a terrible idea to use dynamically named variables.
更多回答(1 个)
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])
Each row of the output array corresponds to the rows in allO. For example, output(1,:) is the example you gave for O1.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!