Plotting a mean line on a graph

198 views (last 30 days)
Om
Om on 19 Feb 2016
Answered: jgg on 19 Feb 2016
Hi everyone
I currently have a graph that looks like this
figure plot(t,r(ix,:),'r'); hold on plot(t,r(~ix,:),'b'); xlabel('Time (ms)') ylabel('R (Y Axis)')
It is essentially a sine graph that tapers off.
What I want to do is add a line where matlab will plot the mean,
One for the value x, one for the value ~x, and one overall.
Please can someone help?

Answers (1)

jgg
jgg on 19 Feb 2016
%set up
x = 1:0.1:10;
r = sin(x);
t = 1:91;
x = mod(t,2) == 0;
m_x = mean(r(x));
m_x2 = mean(-r(~x));
% plotting
figure
plot(t(x),r(x),'b')
hold on
plot(t(~x),-r(~x),'r')
plot(t,ones(length(t),1)*m_x)
plot(t,ones(length(t),1)*m_x2)
You can do it like this; the only operative part you need to do is:
m_x = mean(r(x));
plot(t,ones(length(t),1)*m_x)
Which in your code would like slightly different since you appear to have r as a matrix instead of a vector.

Community Treasure Hunt

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

Start Hunting!