Effacer les filtres
Effacer les filtres

Skipping certain values in an array but still continuing the array

9 vues (au cours des 30 derniers jours)
Sandip Ghatge
Sandip Ghatge le 28 Juin 2020
Commenté : Sandip Ghatge le 28 Juin 2020
I have written a code to skip negative values, but it gives an error, i only want positive values of an array while the negative values must be skipped. (I also dont want zeros in place of these negative values, i just want an array without the negative values)
The code is,
y = zeros(100,1);
for i = 1:100
j = i/10;
y(i,1) = sin(j);
if y(i,1)<0
y(i,1) = [];
end
end
But this gives error
I also tried using the 'break' command, but it gives all zeros just after the first negative value has been encountered.
Can you please suggest a way out?

Réponse acceptée

madhan ravi
madhan ravi le 28 Juin 2020
y = nonzeros(sin(1:1e2));
  5 commentaires
madhan ravi
madhan ravi le 28 Juin 2020
y = nonzeros((sin(1:1e2)>0) .* sin(1:1e2));
Sandip Ghatge
Sandip Ghatge le 28 Juin 2020
Thank you, that gives the desired answer.

Connectez-vous pour commenter.

Plus de réponses (0)

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!

Translated by