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 Center および File ExchangeLoops 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