[Scilab-users] Converting string months to month numbers

Jens Simon Strom j.s.strom at hslmg.de
Fri Oct 21 10:49:16 CEST 2016


Hi Scilabers,
Rafael's approach motivated me to share the following function with you.


function  y=findbystr(x,X,Y)//find Y(x) from Y(X), X and x beeing string matrices
     //X: keys to Y, matrix of strings
     //Y: values allocated to X, matrix of constants, number of rows and colums like X
     //x: argument keys, matrix of strings which are elements of X
     //y: values for x, matrix of constants, numbers of rows and colums like x
     // findbystr() finds numerical values y by their string keys x.  The string number allocation is defined bei X and Y.
   execstr(X+'='+string(Y))
   y=evstr(x)   
endfunction

//Example
X=['a','b';'c','d']
Y=[  10,      20;    30,    40]
y=findbystr(['d','c','b';'c','b','a'],X,Y)


The function would have been useful to me in the past several times -  
and certainly in the future.

Regards
Jens
-------------------------------------------------------------------------
Am 20.10.2016 19:08, schrieb Rafael Guerra:
> Hi Scilaber's,
>
> Fyi, another approach using execstr + evstr functions:
>
> // use month names in M to create 12 variables that are assigned to some 12 numbers
> -->execstr(M + '=' + string(1:size(M,2)));
> -->evstr(Ms)
>   ans  =
>      12.    4.
>      2.     12.
>      6.     7.
>
> This different approach will be much slower than using grep for very large input matrices.
> However, it is worth knowing about it because execstr is one of the most powerful Scilab functions (see for instance the Meta programming chapter in: “Programming in Scilab”, by Michael Baudin, September 2011).
>
> Regards,
> Rafael
>
> From: users [mailto:users-bounces at lists.scilab.org] On Behalf Of Mike Page
> Sent: Thursday, October 20, 2016 1:32 PM
> To: Users mailing list for Scilab <users at lists.scilab.org>
> Subject: Re: [Scilab-users] Converting string months to month numbers
>
> Hi Jens,
> grep does something like what you want.
>
> -->[r,w]=grep(Ms,M);
> -->matrix(w,size(Ms))
>   ans  =
>   
>      12.    4.
>      2.     12.
>      6.     7.
>   
> Not sure why grep produces a vector when searching for a matrix, but it seems that way.
> HTH,
> Mike.
>
> On 20 October 2016 at 10:32, Jens Simon Strom <mailto:j.s.strom at hslmg.de> wrote:
> Hallo,
> Given the string vector
>
> M=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
> which implicitly maps the string representation of the months to their number representataion I would like to map any string matrix containing elements of M into a numercal matrix showing the month numbers.
> E. g.
> Ms=['Dec','Apr';'Feb','Dec';'Jun','Jul']
> should be converted to
> Mn=[12,4;2,12;6,7].
>
> Ho can that be done in a vectorial style?
>
> Backward (given Mn) it is easier to me:
> Ms=matrix(M(Mn),size(Mn))
>
> Regards
> Jens
>
>
>
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20161021/78b230e4/attachment.htm>


More information about the users mailing list