Extract the bigger number of values from the previous one in an array

1 vue (au cours des 30 derniers jours)
Ali Najem
Ali Najem le 28 Sep 2022
Commenté : Ali Najem le 28 Sep 2022
Dear experts,
I want to do this kind of loop
Data = rand(1,15);
for i=1:15
if Data(i) < Data(i+1)
X =10;
else
X=0;
end
end
X;
I suppose to get 15 values of X between 10 and 0 but I obtained this error
Index exceeds the number of array elements. Index must not exceed 15.
how can i proceed this loop into
  1 commentaire
Matt J
Matt J le 28 Sep 2022
I suppose to get 15 values of X between 10 and 0 but I obtained this error
There can't be 15 values, because when i=15, Data(i+1) doesn't exist.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 28 Sep 2022
Modifié(e) : Matt J le 28 Sep 2022
Easier to do,
Data = rand(1,15)
Data = 1×15
0.5961 0.1403 0.0904 0.4693 0.6848 0.1834 0.0516 0.4032 0.2979 0.5497 0.9613 0.9228 0.9208 0.8610 0.7979
X=10*(diff(Data)>0)
X = 1×14
0 0 10 10 0 0 10 0 10 10 0 0 0 0

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices 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