1

Edit for clarity:

I have two matrices, p.valor 2x1000 and p.clase 1x1000. p.valor consists of random numbers spanning from -6 to 6. p.clase contains, in order, 200 1:s, 200 2:s and 600 3:s. What I wan´t to do is

Print p.valor using a diferent color/prompt for each clase determined in p.clase, as in following figure.

I first wrote this, in order to find out which locations in p.valor represented where the 1,2 respective 3 where in p.clase

 %identify the locations of all 1,2 respective 3 in p.clase
    f1=find(p.clase==1);
    f2=find(p.clase==2);
    f3=find(p.clase==3);

%define vectors in p.valor representing the locations of 1,2,3 in p.clase     
     x1=p.valor(f1);
     x2=p.valor(f2);
     x3=p.valor(f3);

There is 200 ones (1) in p.valor, thus, is x1=(1:200). The problem is that each number one(1) (and, respectively 2 and 3) represents TWO elements in p.valor, since p.valor has 2 rows. So even though p.clase and thus x1 now only have one row, I need to include the elements in the same colums as all locations in f1.

So the different alternatives I have tried have not yet been succesfull. Examples:

plot(x1(:,1), x1(:,2),'ro')
hold on
plot(x2(:,1),x2(:,2),'k.')
hold on
plot(x3(:,1),x3(:,2),'b+')

and

    y1=p.valor(201:400);
    y2=p.valor(601:800);
    y3=p.valor(1401:2000);

     scatter(x1,y1,'k+')
     hold on
     scatter(x2,y1,'b.')
     hold on
     scatter(x3,y1,'ro')

and

y1=p.valor(201:400);
y2=p.valor(601:800);
y3=p.valor(1401:2000);


plot(x1,y1,'k+')
hold on
plot(x2,y2,'b.')
hold on
plot(x3,y3,'ro')

My figures have the axisies right, but the plotted values does not match the correct figure provided (see top of the question).

Ergo, my question is: how do I include tha values on the second row in p.valor in my plotted figure?

I hope this is clearer!

MatlabNoob
  • 11
  • 5

2 Answers2

1

Values from both rows simultaneously can be accessed using this syntax:

X=p.value(:,findX)

In this case, resulting X matrix will be a matrix having 2 rows and length(findX) columns.

freude
  • 3,632
  • 3
  • 32
  • 51
  • I added my code to the question so you can see how I tried to apply what you said. However, when I do this I get a figure with all the correct y-values, but only 1 and 2 for x-values. My figure is supposed to look something like [this](http://imgur.com/NDfHWM4) – MatlabNoob Sep 20 '13 at 12:49
  • @MatlabNoob if you have the stats toolbox you should check out the gscatter function – Dan Sep 20 '13 at 14:27
1
M = magic(5)

M =

   17   24    1    8   15
   23    5    7   14   16
    4    6   13   20   22
   10   12   19   21    3
   11   18   25    2    9

M2 = M(1:2, :)

M2 =

   17   24    1    8   15
   23    5    7   14   16

Matlab uses column major indexing. So to get to the next row, you actually just have to add 1. Adding 2 to an index on M2 here gets you to the next column, or adding 5 to an index on M

e.g. M2(3) is 24. To get to the next row you just add one i.e. M2(4) returns 5.To get to the next column add the number of rows so M2(2 + 2) gets you 1. If you add the number of columns like you suggested you just get gibberish.

So your method is very wrong. Freude's method is 100% correct, it's much easier to use subscript indexing than linear indexing for this. But I just wanted to explain why what you were trying doesn't work in Matlab. (aside from the fact that X=p.value(findX findX+1000) gives you a syntax error, I assume you meant X=p.value([findX findX+1000]))

Dan
  • 45,079
  • 17
  • 88
  • 157
  • Thank you! I tried to do use column major indexing as well, by writing `x1=p.valor(f1);` `y1=p.valor(f1+1);` `plot(x1,y1,'r.')` but the plot I get now have an x-axis spanning from 0 to 600 )i.e the number of columns), when my values only span from -6 to 6. What can I do so that it´s clear it´s the values of the numbers in the specific locations and not the location itself I want to plot? – MatlabNoob Sep 20 '13 at 12:55
  • @MatlabNoob, sorry I'm not following you. – Dan Sep 20 '13 at 13:57
  • Yeah sorry, I don`t follow myself @Dan. I will update my question so that it is more easy to understand – MatlabNoob Sep 20 '13 at 14:22
  • @MatlabNoob OK I think this is what you want: `plot(x1(:,1), x1(:,2),'r.')` – Dan Sep 20 '13 at 14:26
  • Hmm, got me a figure with only three symbols. I have edited now for more clarity. Thank you again @Dan – MatlabNoob Sep 20 '13 at 14:55