Azzera filtri
Azzera filtri

Getting the if or while loop to work

2 visualizzazioni (ultimi 30 giorni)
Mark Loui
Mark Loui il 19 Mar 2021
Modificato: Jan il 19 Mar 2021
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 Commento
Jan
Jan il 19 Mar 2021
Please use the tools above the edit field to format the code. This improves the readability.

Accedi per commentare.

Risposta accettata

Jan
Jan il 19 Mar 2021
Modificato: Jan il 19 Mar 2021
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?

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by