How to find the sum of characters in a cell array?
    3 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Naila Mohamed
 el 10 de Mzo. de 2022
  
    
    
    
    
    Editada: Scott MacKenzie
      
 el 10 de Mzo. de 2022
            I need to find the total number of '/' and 'X' characters in my cell array and store the values in variables called num_strikes nad num_spares. 
When I run the code below, I get this:
Undefined function 'eq' for input arguments of type 'cell'. 
Error in solution (line 6) if bowling(k)=='/'
bowling=cell(1,2)
bowling= {{7,2,8,'/',6,2,'X',9,'/',9,0,'X','X',8,1,7,'/',9}, {'X',9, '/','X',8,'/','X','X',7,2,'X',8,'/',9,0}}
k=0;
num_strikes=0;
for k=1:length(bowling)
    if bowling(k)=='/'
        k=k+1
        num_strikes=k
    end
end
disp(num_strikes)
0 comentarios
Respuesta aceptada
  Scott MacKenzie
      
 el 10 de Mzo. de 2022
        
      Editada: Scott MacKenzie
      
 el 10 de Mzo. de 2022
  
      bowling= {{7,2,8,'/',6,2,'X',9,'/',9,0,'X','X',8,1,7,'/',9}, {'X',9, '/','X',8,'/','X','X',7,2,'X',8,'/',9,0}};
num_spares = sum(strcmp([bowling{:}],'/'));
num_strikes = sum(strcmp([bowling{:}],'X'));
disp(num_spares);
disp(num_strikes);
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!

