[scilab-Users] problem with creating loop

Mike Page Mike at Page-One.Waitrose.com
Sat Aug 14 19:43:47 CEST 2010


Hi Marcin,

You can find the unique elements of your first column using the unique
function like:

-->u=unique(xy_w(:,1))
 u  =

    1.
    4.
    6.

Then you can iterate over those values using:

-->for x=u'
-->av = mean(xy_w(find(xy_w(:,1)==x),2:3),'r')
-->end
 av  =

    21.    21.
 av  =

    44.    46.
 av  =

    35.    35.

Hope that helps.

Mike.


  -----Original Message-----
  From: marcin.wozniczka Gazeta.pl [mailto:marcin.wozniczka at gazeta.pl]
  Sent: 14 August 2010 18:05
  To: users at lists.scilab.org
  Subject: Re: [scilab-Users] problem with creating loop


  Ok I understand, but what can I do if the numbers in the first column are
not 1, 2, 3 etc., for example matrix a is looking like that:
  xy_w = [...
  6 10 10
  1 20 20
  4 40 40
  4 50 50
  6 60 60
  4 42 48
  1 22 22 ];


  I modified your function for that:
  nr = a(:,1);      //This is wrong
  for i=1:3        //This is wrong
    ps = nr(i);
    for j=1:3
      av(i,j) =mean(a(a(:,1) == ps,j));
    end
  end


  but I have problem to define final value of 'i'. I know that it's
conecting with 'nr' so if you know how I can declare only once the numbers
from first column (only numbers: 6, 1 & 4)


  --
  Marcin Woźniczka







    Hi Marcin,



    There are off course various ways to solve your problem. Probably the
shortest way to do is like this:



    for it=1:3;

       average(it) =mean(a(a(:,1)==it,2:$));

    end



    It loops over the elements in the first column. The second line is a bit
more cryptic but can be split in separate line:

    msk = a(:,1)==it;                                // creates a boolean
mask vector

    anew = a(msk,2:$);                          // creates a new matrix
using the mask over the second to last columns of a

    average=mean(anew);                  // calculate the average of the
matrix



    Cheers



    Francis









    From: marcin.wozniczka Gazeta.pl [mailto:marcin.wozniczka at gazeta.pl]
    Sent: 12 August 2010 20:59
    To: users at lists.scilab.org
    Subject: [scilab-Users] problem with creating loop



    Hello,

    I have small problem with my program/function in Scilab. I want to loop
function - the loop must be repeated until all datas will be use. For
example I have matrix 'a'

    a = [1 10 20

           2 20 30

           3 30 40

           1 20 10

           3 40 40
           2 20 10]

    and I'm taking all lines which have in the first column the same number,
take average of this and put to the other matrix. I created function which
can do all this thing but I don't have any idea how to make a reapeted loop
from this.



    Can you help me?


    --
    Marcin Woźniczka


    This e-mail, including any attachments and response string, may contain
proprietary information which is confidential and may be legally privileged.
It is for the intended recipient only. If you are not the intended recipient
or transmission error has misdirected this e-mail, please notify the author
by return e-mail and delete this message and any attachment immediately. If
you are not the intended recipient you must not use, disclose, distribute,
forward, copy, print or rely on this e-mail in any way except as permitted
by the author.   ­­



  --
  Pozdrawiam,
  Marcin Woźniczka
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20100814/2c2295ab/attachment.htm>


More information about the users mailing list