how can i use for loop to solve the below problem

Add an if statement to the for loop body. If speed(c) is less than or equal to 0, create a loglog plot of s against lambda using a dashed line (--).
After the if statement, add the command hold on so that only one plot is created.
the below didnt work for me:
if speed(c) <= 0
loglog(lambda,s,"--")
end
hold on
end

댓글 수: 3

Chirstopher - please clarify what you mean by the below didn't work for me. The code seems correct (unless you should be plotting loglog(s,lambda,'--')?) so please post any error messages or discuss why it isn't doing what you expect.
Christopher Achor
Christopher Achor 2020년 5월 22일
편집: Christopher Achor 2020년 5월 22일
Here is the error message as attached.
for c = 1:7
s = spectra(:,c)
if speed(c) <=0
loglog(lambda,s,"--")
end
hold on
end

댓글을 달려면 로그인하십시오.

 채택된 답변

Abdolkarim Mohammadi
Abdolkarim Mohammadi 2020년 5월 22일

0 개 추천

You have used an extra end. Delete the end in the last line. I think you must use hold on before your for loop and place the if inside the for loop.

추가 답변 (2개)

Damrudhar
Damrudhar 2023년 1월 20일

0 개 추천

Add an else statement. If speed(v) is greater than 0, create a plot of s against lambda using a line width of 3.
After the for loop, enter hold off.

댓글 수: 1

for v = 1:7
s = spectra(:,v)
if speed(v) <=0
plot(lambda,s,"--")
else speed(v) >0
plot(lambda,s,LineWidth=3)
end
hold on
end
hold off

댓글을 달려면 로그인하십시오.

Khotso
Khotso 2025년 8월 23일

0 개 추천

for v = 1:7 s = spectra(:,v) if speed(v) <=0 plot(lambda,s,"--") else speed(v) >0 plot(lambda,s,LineWidth=3) end hold on end hold off

카테고리

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

제품

릴리스

R2020a

태그

질문:

2020년 5월 22일

답변:

2025년 8월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by