Probably anyone with programming experience can help me!!

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Probably anyone with programming experience can help me!!

athan
hello I have a little snug!

i have a big list with 4 arithmetic variables in 4 columns, the first two are categorical and the two last are x y coordinates
so..

i want to plot a new 2D figure for each different value of the first variable

and for each figure a new line (plot of x and y) for each different value of the second variable.

i know how to plot lines point etc.
but i am searching a convenient way to separate the data for each line to be plotted
this is probably something quite simple but my programming skills are not quite there.

 thanks a lot
Reply | Threaded
Open this post in threaded view
|

Re: Probably anyone with programming experience can help me!!

Judd Storrs-2
On Fri, Feb 17, 2012 at 5:24 PM, athan <[hidden email]> wrote:
> i know how to plot lines point etc.
> but i am searching a convenient way to separate the data for each line to be
> plotted
> this is probably something quite simple but my programming skills are not
> quite there.

One way to do this is to use a boolean vector to index your data
matrix. Example:

This is a matrix that's similar to what you describe:

octave:1> X = [ 1 1 1.1 2.1 ; 1 2 1.2 4.3 ; 2 1 1.3 5.2 ; 2 1 0.9 1.0
; 1 2 7.5 -9.1]
X =

   1.00000   1.00000   1.10000   2.10000
   1.00000   2.00000   1.20000   4.30000
   2.00000   1.00000   1.30000   5.20000
   2.00000   1.00000   0.90000   1.00000
   1.00000   2.00000   7.50000  -9.10000

One simple way to do this is to extract the two categorical columns as vectors:

octave:2> c1 = X(:,1)   # 1st column
c1 =

   1
   1
   2
   2
   1

octave:3> c2 = X(:,2)   # 2nd column
c2 =

   1
   2
   1
   1
   2

Then if you do a boolean operation like:

octave:4> c2 == 1
ans =

   1
   0
   1
   1
   0

You end up with a boolean vector with 1 (i.e. true) wherever the
second column equals 1 and 0 (i.e. false) everywhere else. You can use
boolean vectors to index. When you do this you get a matrix that
includes the true rows and excludes the false rows. For example:

octave:5> X(c2==1,:)
ans =

   1.00000   1.00000   1.10000   2.10000
   2.00000   1.00000   1.30000   5.20000
   2.00000   1.00000   0.90000   1.00000

gives you all the rows of X where the second column is 1 and

octave:6> X(c1==2 & c2==1, :)
ans =

   2.00000   1.00000   1.30000   5.20000
   2.00000   1.00000   0.90000   1.00000

gives all the rows where the first column is 2 and the second column is 1.

As always, depending on exactly how big your data table is and whether
or not it has some sort of structure special structure, there may be
much more efficient or faster ways to do this.


--judd
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Probably anyone with programming experience can help me!!

athan
thanks for your fast reply!!

your suggestion is very clear and very useful,
but i have two problems

1) The values of the categorical variables are not fixed

2) There are a lot of them

I can not think a way to get a subset of the data
for each different combination of the two categorical variables, without knowing them.

Because the amount of graphs to be plotted is big, I was hoping for some loop,
so I can get a different figure for each value of the first category
and plot a different line for each value of the second category,
without the need to use certain conditional values for each case.

I tried a combination of for and ifs and one with while but I can't set right
Reply | Threaded
Open this post in threaded view
|

Re: Probably anyone with programming experience can help me!!

Judd Storrs-2
On Sat, Feb 18, 2012 at 1:30 AM, athan <[hidden email]> wrote:
> I can not think a way to get a subset of the data
> for each different combination of the two categorical variables, without
> knowing them.

I think you probably want the unique() function.

octave:10> unique(c1)
ans =

   1
   2

octave:11> unique(c2(c1==1))
ans =

   1
   2

octave:12> unique(c2(c1==2))
ans =  1

octave:13> for i=unique(c1)'  # note the ' -- need values as a row
>    for j=unique(c2(c1==i))'
>       X(c1==i & c2==j, :)
>    endfor
> endfor
ans =

   1.0000   1.0000   1.1000   2.1000

ans =

   1.0000   2.0000   1.2000   4.3000
   1.0000   2.0000   7.5000  -9.1000

ans =

   2.00000   1.00000   1.30000   5.20000
   2.00000   1.00000   0.90000   1.00000

This is probably an improvement:

octave:14> for i=unique(c1)'
>    X2 = X(c1==i,:);
>    c2 = X2(:,2);
>    for j=unique(c2)'
>       X2(c2==j,:)
>    endfor
> endfor
ans =

   1.0000   1.0000   1.1000   2.1000

ans =

   1.0000   2.0000   1.2000   4.3000
   1.0000   2.0000   7.5000  -9.1000

ans =

   2.00000   1.00000   1.30000   5.20000
   2.00000   1.00000   0.90000   1.00000


--judd
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Probably anyone with programming experience can help me!!

athan
Thank you very much!!!

 it worked fine!!

I knew it was something basic,
but I couldn't imagine it...