MATLAB := Syntax?

196 views Asked by At

I am having trouble trying to debug some code (new to MATLAB). I have come across the following line of code:

CC[theta_] := {{Cos[theta],I Sin[theta]},{I Sin[theta], Cos[theta]}}

I get an error stating:

Parse error at ":": Usage may be invalid MATLAB syntax.

When I remove the :, I get the exact same message, just that in this case it says 'Parse error at "="'. Is there any way to fix this? This is fairly old MATLAB code so I'm not sure if the syntax style has changed.

1

There are 1 answers

2
Matt On BEST ANSWER

The provided code origins clearly from Mathematica. To do the same in Matlab, we can define a function handle:

CC = @(theta) [cos(theta), 1i*sin(theta); 1i*sin(theta), cos(theta)]

1i is the imaginary unit in Matlab (I in Mathematica). Matlab uses [a,b;c,d] to define a 2x2-matrix ({{a,b},{c,d}} in Mathematica). The definition := is not possible in Matlab. A function handle (as in the example) or a symbolic function can be used.

Evaluated at theta=pi/4 with the command CC(pi/4) gives this result:

 0.7071 + 0.0000i   0.0000 + 0.7071i
 0.0000 + 0.7071i   0.7071 + 0.0000i