how to find index of string using AND operator when adding conditions

조회 수: 1 (최근 30일)
Roxan
Roxan 2020년 8월 13일
댓글: Roxan 2020년 8월 13일
I want to search in a string matrix zz for index of 'A' in a cell when the value of the other cell is 'B'.
using idx = find(ismember(zz, 'A')); I can only get the index A
but i need to know the index of A only if the value of the other cell in another cloumn is B
something like this
idx = find(ismember((zz, 'A')&& (zz,'B')));
Thanks,
Roxan

채택된 답변

Stephen23
Stephen23 2020년 8월 13일
편집: Stephen23 2020년 8월 13일
>> zz = {'2','B','A';'2','C','A';'2','V','H';'2','B','Y';'3','F','A';'3','G','A';'3','B','A';'2','G','A'}
zz =
'2' 'B' 'A'
'2' 'C' 'A'
'2' 'V' 'H'
'2' 'B' 'Y'
'3' 'F' 'A'
'3' 'G' 'A'
'3' 'B' 'A'
'2' 'G' 'A'
>> idx = find(strcmp(zz(:,2),'B')&strcmp(zz(:,3),'A'))
idx =
1
7

추가 답변 (1개)

Bruno Luong
Bruno Luong 2020년 8월 13일
편집: Bruno Luong 2020년 8월 13일
Your description is not clear to me so I give two versions
idx = find(ismember(zz, {'A' 'B'}));
or
idx = find(ismember(zz, 'A') & ismember(xx, 'B'));
  댓글 수: 1
Roxan
Roxan 2020년 8월 13일
thanks for your reply.
The second syntex return an empty index. The first syntax will return the index of both A and B. but what is need is to find the index of A only if the other cell value is B.
see below:
"2" "B" "A"
"2" "C" "A"
"2" "V" "H"
"2" "B" "Y"
"3" "F" "A"
"3" "G" "A"
"3" "B" "A"
"2" "G" "A"

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

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by