v1(v1 < 4) = 0;
modify v1 so that any value less than 4 is replaced with the value 0. Please help me making code.
    11 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Engr.Syed Hassan Iqbal Shah
 le 15 Juin 2024
  
    
    
    
    
    Déplacé(e) : Voss
      
      
le 17 Juin 2024
            modify v1 so that any value less than 4 is replaced with the value 0. please help me writing code.
1 commentaire
Réponse acceptée
  Ganesh
      
 le 15 Juin 2024
        v1 = randi([1 6],1,10) % Example data
% Replace any value less than 4 with 0
v1(v1 < 4) = 0
We use "vector indexing" to achieve this. 
When you apply a "condition" on an array, the result will be a logical array where, the corresponding indices in the result will be a 1 or a 0. You can use the result array to apply it back on v1 and modify the values where the "logic" holds true. You can use this to compare an array to a value, or even two equal sized arrays and their corresponding index values! You can find more on this using the following link:
2 commentaires
  Ganesh
      
 le 15 Juin 2024
				Importantly, all of this would be covered in the free Self Paced Online Course, MATLAB Onramp. It takes only about two hours, a fair tradeoff to asking a question here and getting the answer an hour later.
Plus de réponses (1)
Voir également
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!