-2

I have a matrix with a set points, divided in groups of 10 (example below). Each group of points corresponds to a line; how can I plot all lines?

Here is an example of how the matrix is organised:

y = [
109.41  110.55  111.69  112.83  113.96  115.10  116.24  117.37  118.51  119.65
56.87   56.21   55.55   54.89   54.23   53.57   52.91   52.25   51.5    50.92
-265.16 -263.07 -260.99 -258.90 -256.81 -254.73 -252.64 -250.55 -248.47 -246.38 ];

This is the code I am using to produce the matrix and try to plot all the lines:

for line = (1:n)
    for point = (1:10) 
        y(line,point) = [Y(line)-point*sin(Omega(line))];
    end
end

plot(0:1000,y,'linewidth',2)
Shai
  • 111,146
  • 38
  • 238
  • 371
albus_c
  • 6,292
  • 14
  • 36
  • 77
  • I edited the question, adding the code I am using now. – albus_c Oct 27 '14 at 14:39
  • Please, try not to [use `i` and `j` as variable names in Matlab](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab), especially [`j*sin()`](http://en.wikipedia.org/wiki/Euler's_formula) can be very misleading... – Shai Oct 27 '14 at 14:45
  • What is `n`? what is `Y` and `Omega` how are they relate to the 10x3 numbers you posted at the beginning of your question? Please, take a minute and edit your question to make it sensible. – Shai Oct 27 '14 at 14:47
  • what is wrong with the plot you got from the `plot` command? – Shai Oct 27 '14 at 14:48
  • I get the error `Error using plot Vectors must be the same length.` – albus_c Oct 27 '14 at 14:49
  • I suppose `n` is 1000? – Shai Oct 27 '14 at 14:50

2 Answers2

1

Assuming Y is of size 1-by-n, Omega is of size 1-by-n, then you can avoid the nested loop:

 y = bsxfun( @minus, Y, bsxfun( @times, (1:10)', sin( Omega ) ) ); %'
 plot( 1:n, y, 'LineWidth', 2 );
Shai
  • 111,146
  • 38
  • 238
  • 371
1

I am not surprised you got the error you have with the code you are using. size(0:1000) is 1x1001. What size is your matrix y?

With the data you have provided, I would use the following:

y = [109.41  110.55  111.69  112.83  113.96  115.10  116.24  117.37  118.51  119.65; ...
56.87   56.21   55.55   54.89   54.23   53.57   52.91   52.25   51.5    50.92; ...
-265.16 -263.07 -260.99 -258.90 -256.81 -254.73 -252.64 -250.55 -248.47 -246.38];

plot(0:100:900,y,'linewidth',2) % size(0:100:900) is 1x10 and size(y) is 3x10 so we're good

This gives the following result (in Octave, should be exactly the same in MATLAB):

enter image description here

am304
  • 13,758
  • 2
  • 22
  • 40