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

조회 수: 2 (최근 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
  댓글 수: 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 CenterFile Exchange에서 Loops 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