How to plot a repeated y value after some interval vaue of x?

2 次查看(过去 30 天)
I have (x,y) data but I want to repeat the y value after each multiple of 9 up to the last value of xn, i.e x= 100. I expect a triangular shape with touching at x =[1 9 18 29 etc ] . i tried with the following code but not effective , any one who can show my error . thanks
y=[1 2 3 4 5 4 3 2 1]
x=[1 2 3 4 5 6 7 8 9]
xn=ones(1,100);
j = 1;
for i = 1:length(xn)
if(j==10)
j = 1;
end
if i<=5
y(i)=i;
else
y(i)=y(5)-y(i-5);
end
j = j+1;
plot(x(i), y(j));
end
error
Index exceeds matrix dimensions.

回答(1 个)

Walter Roberson
Walter Roberson 2018-1-26
for i = 1:length(xn)
...
plot(x(i), y(j));
for that plot to work, length(x) must be at least length(xn) . But length(xn) is 100, and length(x) is 9.
I do not see any use for xn except to provide length() for the for i loop. Why not just directly code for i = 1 : 100 ?
  2 个评论
seble mekonnen
seble mekonnen 2018-1-26
yes, right but I want to use xn for any arbitrary size, for instance for simulation time of xn. and to plot the (x,y)
Walter Roberson
Walter Roberson 2018-1-26
Then perhaps you want
plot(xn(i), y(j), '*');
hold on;
However, all of your xn are 1, so it is not clear this is what you want.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by