ploting a 2D graph with multiple lines

Hi, I'm a bigginer and what I need is too specific.
I need to plot a graph that looks exaclty like this:
I have a h vector of size 4. a matrix X of size 4x39 and a matrix f(x) of size 4x39.
each line represents the ploting of row i in matrix X with corresponding row i in matrix f(x) for h(i).
Can someone please tell me how to plot this?
Thank you

 采纳的回答

Something like this
X; % 4x39
f; % 4x39
h; % 4x1
plot(X.', f.')
xlabel('x')
ylabel('(approximate) f(x)')
legend_strs = compose('Approximate f with h=%.2f', h)
legend(legend_strs)

6 个评论

Ameer, Thank you! this is almost exactly what I need. My graph now looks like this:
The sharp staright lines are there because the X matrix is 4x39 but only the 39th row (represented by the purpule line in the graph) has 39 elements. the other rows are shorted. but in order to define the matrix I had to fill on the empty cells with zeros.
so, if row i in X has 39 values and row j has 39-n values and the n last cells in the row are zeros, how can I not plot all the zeros in my graph?
Thank you
I didn't understand it correctly. Can you share your data in a .mat file?
Yes! thank you.
I have two variables in F_X_data.mat.
U is equivalent to f in your above answer. and X_in is equivalent to X.
row i in X_in is the x axis of line i. and row i in U is the y axis of line i.
as you can see, all of the rows (in both X_in and U) have the same length. But, that is only because I initialized the matrices with zeros.
If row i has non zero values in it's first n cells, then the rest of the row is filled with zeros. and that is what is causing these sharp straight lines to zero in my graph:
I need my graph to not "see" the zeros. and thus, not plot them.
How can I handle this issue?
Thank you
Just replace 0 with nan. They will not be visible in plot()
figure();
ax = axes();
hold(ax);
U(U==0) = nan;
X_in(X_in==0) = nan;
for i = 1:4
plot(X_in(i,:), U(i,:))
end
I am glad to be of help!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Line Plots 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by