I don't know why my plot is wrong?

1 次查看(过去 30 天)
Hi,
My question is the following:
Write a script noisify.m where you first create an x vector that has integer elements from 1 to 10, and then set a y vector equal to x. Plot this straight line. Now, add noise to the data points. Create a new y2 vector that stores the values of y plus or minus 0.25 (Hint: y2 is of the same length as y. Each element of y2 is either larger or smaller than the corresponding element of y by 0.25. Choose whether to be larger or smaller randomly) Plot the straight line and also these noisy points(using black stars for the marker points).
x=1:10;
y=x;
plot(x,y,'b');
hold on
a= 0.25* randi([0 1],1,10)- 0.25;
y2=y+a;
plot(x,y2,'k *')
i don't get why the plot is not similar to the one in my assignment?

采纳的回答

Image Analyst
Image Analyst 2016-9-25
To get +/- 0.25, you need to have a range of 0.5. So change your code to be like this:
x = 1 : 10;
y = x;
plot(x, y, 'b');
hold on
additiveNoise = 0.5 * randi([0, 1], 1, length(y)) - 0.25;
y2 = y + additiveNoise;
plot(x, y2,'k*')
grid on;
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by