find ( strcmp ( many_different_elements )

조회 수: 1(최근 30일)
Dave
Dave 2021년 10월 14일
편집: Stephen 2021년 10월 14일
Hello, I want to check if any of the elements of a cell CC with 90 elements are in column2 of a matrix w 10k rows. If so, to retrieve the row location.
CC below has four elements
CC={'AB1', 'AB2', 'AB3' ,'AB4'}
I would use
idloc = find(strcmp(databig{:,2}, 'AB1') | strcmp(databig{:,2}, 'AB2') | strcmp(databig{:,2}, 'AB3') | strcmp(databig{:,2}, 'AB4') )
But was wondering if there's a nicer way than to write 90 times in the find line
  댓글 수: 2
Mitchell Thurston
Mitchell Thurston 2021년 10월 14일
I just noticed the end of the first sentence also wanted the row location. The general idea should still work.

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

답변(2개)

Ryan
Ryan 2021년 10월 14일
You could create a for loop that checks each element and then do the find outside the four loop for find the rows. Something like this
CC = {'AB1', 'AB2', 'AB3' ,'AB4'};
for ii = 1:length(CC)
idx(:,ii) = strcmp(databig,CC{ii});
end
[idloc,~] = find(idloc);

Stephen
Stephen 2021년 10월 14일
편집: Stephen 2021년 10월 14일
"But was wondering if there's a nicer way than to write 90 times in the find line"
Of course: forget about repeated STRCMP calls (e.g. in a loop), just use one ISMEMBER call:
Read its documentation and pay careful attention to the order of its inputs and outputs.
X = ismember(databig(:,2),CC)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by