I don't know why my plot is wrong?

조회 수: 1 (최근 30일)
Sarah Sadeq
Sarah Sadeq 2016년 9월 25일
답변: Image Analyst 2016년 9월 25일
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