Effacer les filtres
Effacer les filtres

How to add a number to an element of a vector which meets a certain criteria?

2 vues (au cours des 30 derniers jours)
I have a vector which has sleep times. Its length is 915. But just for an example lets say,
t=[21.33; 22.45; 23.11; 23.67; 0.13; 1.56; 2.33];
The last 3 elements are times after midnight. So I want to add 24 to times after midnight. How can I do it? I tried using if statement.
if t<5
t=t+24
end
but this doesn't work.

Réponse acceptée

Star Strider
Star Strider le 3 Sep 2015
This works:
t=[21.33; 22.45; 23.11; 23.67; 0.13; 1.56; 2.33];
t(t<5) = t(t<5)+24
t =
21.33
22.45
23.11
23.67
24.13
25.56
26.33
This approach uses ‘logical indexing’ to specifically address only the elements you want. (Using decimal notation for the time makes this much easier!)

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by