Check if string matches many possibilities or check if doesn't match few possibilities.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Christopher Wong
el 2 de Nov. de 2020
Comentada: Christopher Wong
el 2 de Nov. de 2020
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.
0 comentarios
Respuesta aceptada
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)
Ver también
Categorías
Más información sobre Characters and Strings 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!