How to link markers with lines in for loop

조회 수: 2 (최근 30일)
Mohammed Sadeq
Mohammed Sadeq 2016년 1월 14일
댓글: harjeet singh 2016년 1월 16일
When I plot a graph using for loop with Matlab 2015a as below:
The plot look like this:
I want to connect each 'n' with line as follows:
How to do that?
Thanks..

채택된 답변

harjeet singh
harjeet singh 2016년 1월 14일
try to use this code
clear all
close all
clc
X=randint(5,100,[1 1000]);
Y=randint(1,100,[1 1000]);
for n=1:100
x=[];
y=[];
for k=1:5
x=[x X(k,n)];
y=[y Y(1,n)];
end
figure(1)
plot(y,x,'-o')
hold on
drawnow
end
  댓글 수: 4
Mohammed Sadeq
Mohammed Sadeq 2016년 1월 15일
I am not sure what you are doing @Stephen, are you here to answer the question or criticize other answers!
@harjeet, Thanks for your answer. I will accept it
harjeet singh
harjeet singh 2016년 1월 16일
thanks @mohammed for accepting answer.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2016년 1월 14일
편집: Stephen23 2016년 1월 14일
Solution: learn to program MATLAB code without using loops everywhere.
Although beginners think that loops are great for solving all of their problems, in fact MATLAB works best when you learn to perform your operations on all of arrays at once, in particular by writing vectorized code. So you can just pass the plot function your data matrices, and it will plot the columns of that matrix. This is clearly explained in the plot documentation.
>> X = [0.1,0.1,0.2,0.3,0.5;1.1,1.1,1.1,1.2,1.4]';
>> Y = [5,4,3,2,1;4,3,2,1,0]';
>> plot(X,Y,'-o')
>> grid on
>> legend({'one','two'})
See, no loops!
  댓글 수: 3
Stephen23
Stephen23 2016년 1월 14일
편집: Stephen23 2016년 1월 14일
Your data is so huge that plot cannot handle it?
Interesting, because although plot will easily handle many millions of points (I just tried 1e7), it is senseless to attempt to plot so many points:
  • plotting more points than screen-pixels can show means that any extra points are just a waste of plotting power, and do not show in the final figure.
  • plotting many different curves results in a figure that is impossible for any human to make sense of.
And both of these will slow down your graphics thread, etc. In general removing loops and replacing these with vectorized code will make code faster, and more expandable to larger array sizes.
What do you imagine that you will see in a figure with millions of points?
How "huge" is your data? Can you please be more specific, i.e. give us a an actual size, and tell us why you need to plot millions of points.
Mohammed Sadeq
Mohammed Sadeq 2016년 1월 15일
@Stephen, I believe you didn't understand my problem and your last comment prove it. Anyway, thanks for trying..

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by