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

1 visualización (últimos 30 días)
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 comentario
Matt J
Matt J el 28 de Sept. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 28 de Sept. de 2022
Editada: Matt J el 28 de Sept. de 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

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices 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!

Translated by