Loopless function calls on vector/matrix members in Matlab/Octave
        Posted  
        
            by Sint
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Sint
        
        
        
        Published on 2010-03-18T15:06:18Z
        Indexed on 
            2010/03/18
            15:11 UTC
        
        
        Read the original article
        Hit count: 606
        
I came into matrix world from loop world(C, etc)
I would like to call a function on each individual member of a vector/matrix, and return the resulting vector/matrix.
This is how I currently do it:
function retval = gauss(v, a, b, c)
  for i = 1:length(v)
    retval(i) = a*(e^(-(v(i)-b)*(v(i)-b)/(2*c*c)));
  endfor
endfunction
Example usage:
octave:47> d=[1:1000];
octave:48> mycurve=gauss(d, 1, 500, 100);
Now, all advice on Matlab/Octave says: STOP whenever you catch yourself using loops and think of a better way of doing it.
Thus, my question: Can one call a function on each member of a vector/matrix and return the result in a new vector/matrix all at once without using explicit loops?
That is I am looking for something like this:
 function retval = newfun(v)
    retval = 42*(v^23); 
endfunction
Perhaps, it is just syntactic sugar, that is all, but still would be useful to know.
© Stack Overflow or respective owner