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일

0 개 추천

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개)

카테고리

도움말 센터File Exchange에서 Simulink에 대해 자세히 알아보기

질문:

2020년 10월 6일

댓글:

2020년 10월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by