How to get first value of an array with condition
    20 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Sharah
 el 11 de Jul. de 2018
  
    
    
    
    
    Comentada: Adam Danz
    
      
 el 31 de Mzo. de 2022
            I need to get the first value of a matrix (x2 below) that I initially put condition to, for example:
    x1 = Data{i}.Error(something == 1, 1)
    x2 = x1(1)
I need to do this within one step without creating `x1', is it possible? Obviously
Data{i}.Error(something == 1, 1)(1)
won't work
2 comentarios
  Brian Katz
 el 31 de Mzo. de 2022
				How would you do the same thing with a table construct? 
Scenario{4}(Scenario{4}.sID==35,{'Groupe'})
  Adam Danz
    
      
 el 31 de Mzo. de 2022
				Assuming Groupe is a table variable, and Scenario{4} is a table, 
Scenario{4}(Scenatrio{4}.Groupe(Scenario{4}.sID==35))
not tested
Respuesta aceptada
  Adam Danz
    
      
 el 11 de Jul. de 2018
        
      Editada: Adam Danz
    
      
 el 11 de Jul. de 2018
  
      By using the 2nd input to find() you can get just the first index of 'Error'.
% fake data
Data{1}.Error = magic(10); 
something = [0 1 0 1 0 1 0 0 0 0]; 
% 2-line method
x1 = Data{1}.Error(something == 1, 1); 
x2 = x1(1);  
% 1-line method
x3 = Data{1}.Error(find(something == 1, 1), 1); 
% Check that they match
x2 == x3
Note that this works if x2 is always the first element of x1. If you're looking for the n_th element of x1 you'll need to replace find() with
max(find(something == 1), n).
0 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Logical 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!


