0

Given the following matlab code:

A = reshape (1:9 ,[3 3]);
f = @(x) x^2;
f(A)

the output is:

A =                                                                                                                                                                                                                                                    

   1   4   7                                                                                                                                                                                                                                           
   2   5   8                                                                                                                                                                                                                                           
   3   6   9  

ans =                                                                                                                                                                                                                                                  

    30    66   102                                                                                                                                                                                                                                     
    36    81   126                                                                                                                                                                                                                                     
    42    96   150 

Can you please explain to me how this output is calculated? how the anonymous function is invoked?

I want to write the same program in python.

Is there any compatible function in python which lets me implement this?

Suever
  • 64,497
  • 14
  • 82
  • 101
maro
  • 3
  • 1
  • Are you trying to do matrix multiplication or assign anonymous functions? – BallpointBen Feb 24 '17 at 19:15
  • 3
    @juanpa.arrivillaga it's the [proper MATLAB term](https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html). – sco1 Feb 24 '17 at 19:21
  • That was suppose to be tongue-in-cheek. – juanpa.arrivillaga Feb 24 '17 at 19:23
  • 1
    @juanpa.arrivillaga Yes, I understand that but the humor will likely be lost on those who do not understand the joke, particularly when it leads off with "that's a pretty lousy anonymous function" and does not terminate with some kind of emote. – sco1 Feb 24 '17 at 21:28

1 Answers1

2

An anonymous function with a matrix as an input behaves just as the contents normally would if executed from the command line. It does nothing special.

The issue is that you're using ^ which is the matrix power (mpower) and I believe you are looking for the element-wise power (power) which is .^ which in your case would square each element individually.

f = @(x)x.^2;
B = f(A);

If you want to do the same in Python, you can do exactly this with numpy

import numpy as np

A = np.arange(1, 10).reshape(3, 3).T
B = np.power(A, 2)

If you want to encaptulate this operation, it's best pratice to put it into it's own function

def f(x):
    return np.power(x, 2)

B = f(A)

If you named lambda (anonymous) function (this isn't recommended) you can do:

f = lambda x: np.power(x, 2)
B = f(A)
Community
  • 1
  • 1
Suever
  • 64,497
  • 14
  • 82
  • 101
  • You should probably point out that in Python, you wouldn't (or rather *shouldn't*) do something like `f = lambda x: np.power(x, 2)`. Anonymous functions are *meant to be anonymous*. You would just do `def f(x): return np.power(x, 2)` – juanpa.arrivillaga Feb 24 '17 at 19:29
  • 1
    @MaroueneBoubakri Why didn't you include the actual function in your question? – Suever Feb 24 '17 at 20:05
  • 2
    @maro: It sounds like you need to read a tutorial on Python and numpy. We can't teach you the entire Python syntax here, not to mention the Python syntax and all the numpy array methods. – TheBlackCat Feb 24 '17 at 20:57