Effacer les filtres
Effacer les filtres

find ( strcmp ( many_different_elements )

11 vues (au cours des 30 derniers jours)
Dave
Dave le 14 Oct 2021
Modifié(e) : Stephen23 le 14 Oct 2021
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 commentaires
Mitchell Thurston
Mitchell Thurston le 14 Oct 2021
I'm not too experienced with strcmp so there might be a better way, but the easiest way I can think of for this would be
per_index = zeros(numel(CC),1);
for i = 1:length(per_index)
per_index(i) = any( strcmp(databig{:,2}, CC{i}) );
end
idloc = any(per_index)
I would also recommend using "any" for each strcmp instead of find
Mitchell Thurston
Mitchell Thurston le 14 Oct 2021
I just noticed the end of the first sentence also wanted the row location. The general idea should still work.

Connectez-vous pour commenter.

Réponses (2)

Ryan
Ryan le 14 Oct 2021
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);

Stephen23
Stephen23 le 14 Oct 2021
Modifié(e) : Stephen23 le 14 Oct 2021
"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)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by