See also the mapslices
<http://docs.julialang.org/en/release-0.4/stdlib/arrays/#Base.mapslices>
function which is very close to your function but more general.

On Wed, Aug 17, 2016 at 12:59 PM, Erik Schnetter <schnet...@gmail.com>
wrote:

> In Julia, you would not map a string, but map a function instead:
>
> ```Julia
> function mapcol(f, p0)
>     M,N = size(p0)
>     y = similar(p0)
>     for i in 1:N
>         x = view(p0, :,i)
>         y[:,i] = f(x)
>     end
>     y
> end
> ```
>
> You would call it e.g. as `mapcol(v -> v+1, p0)`.
>
> Parsing and evaluating a string is also possible, of course, but is
> usually not necessary. Look at the documentation for `parse` if you are
> interested.
>
> -erik
>
>
> On Wed, Aug 17, 2016 at 3:34 AM, Nicholas Hausch <nhau...@gmail.com>
> wrote:
>
>> Hello all. I am attempting to translate a MATLAB file that evaluates a
>> string column by column. The function is simple, pasted below:
>>
>> function y = mapcol(text,p0,p1,p2)
>> %MAPCOL
>> %     mapcol(STRING,A,B,C)  will execute the command
>> %       contained in STRING over the columns of the matrix A,
>> %       creating one column of the output matrix at a time.
>> %       The additional arguments B and C are available to pass parameters.
>> %     EX:  mapcol('x.*p1',A,b) will do a point-wise
>> %          multiplication of each column of A by the vector b.
>> %
>> [M,N] = size(p0);
>> y = [];
>> for i=1:N
>>    x = p0(:,i);
>>    y(:,i) = eval(text);
>> end
>>
>> In trying to write the equivalent function in Julia, I ran into issues
>> regarding eval() only having access to variables at the global level. Is
>> this possible to translate?/ How would I go about doing so (preferably
>> without too much added complexity, which I noticed in other threads
>> regarding the scope of eval)? I realize that this function may not appear
>> to be very useful on the surface, but for me it would be worth having as a
>> tool to use in other functions.
>>
>> Any feedback is appreciated. Thanks.
>>
>
>
>
> --
> Erik Schnetter <schnet...@gmail.com> http://www.perimeterinstitute.
> ca/personal/eschnetter/
>

Reply via email to