# getting the index after comparing two logicals

조회 수: 2(최근 30일)
cgo 2022년 7월 20일
답변: Mathieu NOE 2022년 7월 20일
I compared two logicals and their results are as follows: a = [1 0 0 0 1] and b = [0 0 0 0 1]
1. I wanted to compare these logicals, identifying the corresponding columns where both 1's appear.
2. Identify which column that is: in this case, column 5.
Thanks

댓글을 달려면 로그인하십시오.

### 답변(2개)

DGM 2022년 7월 20일
Something like this:
a = [1 0 0 0 1];
b = [0 0 0 0 1];
idx = find(a & b)
idx = 5
Though depending on your needs, the usage of find() may be an unnecessary complication, as the intersection of the two vectors can likely be used for logical indexing just the same.
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

Mathieu NOE 2022년 7월 20일
hello
see code below :
a = logical([1 0 0 0 1])
b = logical([0 0 0 0 1])
c = a & b
col_c_is_one = find(c)
gives the following results :
a = 1×5 logical array : 1 0 0 0 1
b = 1×5 logical array : 0 0 0 0 1
c = 1×5 logical array : 0 0 0 0 1
col_c_is_one = 5

댓글을 달려면 로그인하십시오.

### 범주

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!