フィルターのクリア

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

2 ビュー (過去 30 日間)
seble mekonnen
seble mekonnen 2018 年 1 月 26 日
コメント済み: Walter Roberson 2018 年 1 月 26 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by