if loop for whole vector

1 次查看(过去 30 天)
HI, I have this code and the vector "hrs" is a 200 x 1 double. I cant seem to get the answer I want with this code. How may I debug this? Thank you so much!
If the vector "hrs" is <10, I want 24 to be added to it. if it is > 10, it can remain as it is
if hrs < 10
hrs = hrs + 24;
else
hrs = hrs;
end

采纳的回答

Ameer Hamza
Ameer Hamza 2020-10-6
编辑:Ameer Hamza 2020-10-6
In MATLAB, you can directly use logical indexing instead of writing a if-block
idx = hrs < 10;
hrs(idx) = hrs(idx) + 24;
In case, you want to see what is wrong with your code, following show how to use if-block with for-loop
for i = 1:numel(hrs)
if hrs(i) < 10
hrs(i) = hrs(i) + 24
else
hrs(i) = hrs(i);
end
end
  2 个评论
Cside
Cside 2020-10-6
Great! this was amazing thank you :)
Ameer Hamza
Ameer Hamza 2020-10-6
I am glad to be of help! :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by