Check if string matches many possibilities or check if doesn't match few possibilities.

2 vues (au cours des 30 derniers jours)
Let's say I have a set of validated strings: {'B2D2', 'CPS4', 'R2D2', 'RB2D2', 'SB2D2', 'T2D2'}. And I want to proceed based on whether the input string matches any of the following: {'B2D2', 'R2D2', 'RB2D2', 'SB2D2'}. Which would be a faster invocation of strcmp()?
if any(strcmp(params.Results.eletype, {'B2D2', 'R2D2', 'RB2D2', 'SB2D2'}))
Or, check if it DOES NOT match the supplementary possibilites:
if ~any(strcmp(params.Results.eletype, {'CPS4', 'T2D2'}))
I ask this from a high-performance standpoint. I'm trying to cut computation time wherever I reasonably can here.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 2 Nov 2020
Modifié(e) : Ameer Hamza le 2 Nov 2020
I guess that 2nd should be faster because it involve less comparisons; however, Instead of speculations, it is better to just profile the code: https://www.mathworks.com/help/matlab/matlab_prog/profiling-for-improving-performance.html.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by