Getting the if or while loop to work

1 次查看(过去 30 天)
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 中查找有关 Logical 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by