Getting the if or while loop to work

조회 수: 2 (최근 30일)
Mark Loui
Mark Loui 2021년 3월 19일
편집: Jan 2021년 3월 19일
Hi i like to ask for some help please
n=20;
iter_h=0;
x=rand(n,1);
for i=1:n-1
h(i,:)=x(i+1)-x(i)
iter_h=iter_h+1
while i ==4
SLPS=[1 0 0 0;0 h(1) 2*(h(2)+h(1)) h(2);0 0 h(2) 2*(h(3)+h(2));0 0 0 h(3)]
end
% if i==19
% SLPS=rand(i,1)
% end
% if n==4
% SPLS_4=[1 0 0 0;0 h(1) 2*(h(2)+h(1) h(2);0 0 h(2) 2*(h(3)+h(2));0 0 0 h(3)]
end
when i use the while loop, the code cannot stop running however if i use the if, it does not show my SLPS matrix as shown, in fact it does not show anything.
My Question and main goals is to create a matrix when the i=4 or n=4 and when i=20, another matrix assuming the matrix using the random but i have another set of function to run.
How can i do so to overcome this problem, as it is very important for me to know how to get the matrix for those two case, other i values are not important if cannot be found a code which can produce as such. Most important points are when i=4 and i=20.
  댓글 수: 1
Jan
Jan 2021년 3월 19일
Please use the tools above the edit field to format the code. This improves the readability.

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

채택된 답변

Jan
Jan 2021년 3월 19일
편집: Jan 2021년 3월 19일
while i == 4
SLPS=[1 0 0 0;0 h(1) 2*(h(2)+h(1)) h(2);0 0 h(2) 2*(h(3)+h(2));0 0 0 h(3)]
end
This is, as you have seen already, an infinite loop: If you do not change the loop counter inside the loop, there is no chance to leave the loop, when it was entered. See:
doc while
You have asked several questions for the same problem. As far as I can see, you did not mention yet, what you want as result. Therefore it is impossible to suggest some code.
Your code does not clarify your intentions also.
How is the general definition of the matrix? You have showed the output for n=4, but how does it look for n=5 or n=20?

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by