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

3 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Star Strider
Star Strider am 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!)

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by