Why is this output nothing and what can i change to make it work

2 次查看(过去 30 天)
n=20;
x=rand(n,1)
y=rand(n,1);
RHS_empty=ones(n:1)
iter_h=0
iter_a=0;
for i=1:n-1
h(i)=x(i+1)-x(i);
iter_h=iter_h+1;
a(i)=y(i);
iter_a=iter_a+1;
RHS=a.*RHS_empty;
while n==4
SPLS=[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)]
c(:,i)=SPLS^-1*a(:,i)
end
% while n == 20
% SPLS=
%
% end
end
There is nothing wrong with the code in MATLAB where no red line was shown but the output was not shown for the SPLS and the c in the command window, can i know why and how can i change on this as i have another while loop to put inside there where the n=20, another set of matrix will be form but was not inserted into it by me as it is very long.

采纳的回答

John D'Errico
John D'Errico 2021-3-18
I had to laugh at your statement that there is nothing wrong with the code. That seems counter to the fact that it produces nothing by your own statement. What it should do I cannot guess, because we are not told what it should produce. The crystal ball is just so foggy today.
In a quick perusal, what I did find interesting was this code fragment:
while n==4
SPLS=[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)]
c(:,i)=SPLS^-1*a(:,i)
end
So most of your code runs inside that while loop. Now, remember n is a scalar variable, that has been set to 20 in the beginning of the code. No place in the code has n ever been modified, so n will NEVER take on the value 4.
Yet, the important part of your code runs ONLY when n==4.
Do you see a fundamental problem here?
  6 个评论
Walter Roberson
Walter Roberson 2021-3-19
n=10
a=2
while n>=3
a=a+1;
break %Can i use stop or is there a function that i can call to stop this section from going forward again
end
But that would be pointless, since you could instead do
n=10
a=2
if n>=3
a=a+1;
end
since you would only be doing the increment once.
Jan
Jan 2021-3-19
@Loui Pinn Wern: Although you can use break also, it is more direct to use the condition for stopping. In your case, the loop is stopped when n < 3. See:
doc while
Please read Matlab's Onramp to learn the basics. This is more efficient than asking them in the forum.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by