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 <[email protected]> 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 <[email protected]>
http://www.perimeterinstitute.ca/personal/eschnetter/