Extracting non zero values from structure variable
    7 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    chandra Naik
 le 26 Oct 2021
  
    
    
    
    
    Commenté : Stephen23
      
      
 le 26 Oct 2021
            Suppose S is a structre variable with following zero and non-zero values , Then How do we extract only non-zero values from structure variable S.
s=100;
for i=1:s
    if (sensorCover(i))
        S(i).E=0.5;
        S(i).xd=xf(i)
        S(i).yd=yf(i)
        S(i).ir=ircount(i)
        S(i).cr=cf(i)
    else 
        S(i).E=0;
        S(i).xd=0;
        S(i).yd=0;
        S(i).ir=0;
        S(i).cr=0;
    end
end
2 commentaires
Réponse acceptée
  Sulaymon Eshkabilov
      
 le 26 Oct 2021
        
      Modifié(e) : Sulaymon Eshkabilov
      
 le 26 Oct 2021
  
      Use logical indexing to take out non-zero values from a sturcture array, e.g.:
S.m(1) =0.1;  S.m(2:5)=0; S.m(6:9)=pi; S.k(1:3)=0; S.k(4:5)=-1;
S.m(S.m~=0)                % Non-zero values
S.k(S.k~=0)                % Non-zero values
3 commentaires
  Stephen23
      
      
 le 26 Oct 2021
				It is simpler to use NONZEROS:
S.m(1)   = 0.1;
S.m(2:5) = 0;
S.m(6:9) = pi;
S.k(1:3) = 0;
S.k(4:5) = -1;
nonzeros(S.m)
nonzeros(S.k)
Note that the original question showed a non-scalar structure with fields of scalar numerics, whereas the answer given above shows a scalar structure with fields of numeric vectors. These are clearly not the same thing.
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Variables 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!