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

2 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 2 de Nov. de 2020
Editada: Ameer Hamza el 2 de Nov. de 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.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by