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

3 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2015-9-3
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!)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by