2

I am trying to convert some matlab code into Python. I have almost no experience with matlab, but I just need to borrow a little functionality. I am stuck on this part:

In this example, V is a 3x3 matrix.

A = V(:,3)  % i.e. A = [1 2 3]
par = [-(A(2:3))'/A(1)]

Specifically, I am puzzled by the use of '.

I have been using this resource to go between matlab and Python: http://mathesaurus.sourceforge.net/matlab-numpy.html However, it is ambiguous as the ' sign appears to have multiple uses. When I search for other documentation, I can't find a comprehensive explanation for '.

Any help would be much appreciated. Ideally, I would like to get the Python equivalent, but any explanation will help. Thanks!

J Jones
  • 3,060
  • 4
  • 26
  • 43
  • 4
    Just the transpose, no? In Python, use ".T" (for Numpy) – Joel Vroom Nov 04 '13 at 23:39
  • Note that if in your implementation `A` is a *one-dimensional* array (e.g. `A.shape == (3,)`), then the transpose doesn't actually do anything. `A.T` is still a one-dimensional array with shape (3,). – Warren Weckesser Nov 05 '13 at 03:50
  • @WarrenWeckesser Not exactly sure what you mean but I can't think of any sensible situation where `A(2:3)` is equal to `A(2:3)'` – Dennis Jaheruddin Nov 05 '13 at 14:47
  • @DennisJaheruddin: I'm talking about numpy arrays. In numpy, the transpose operation simply reverses the dimensions of the array. If the array has only one dimension, the transpose is a "no op"--it doesn't do anything. – Warren Weckesser Nov 05 '13 at 16:15
  • @WarrenWeckesser Hmm in fact `A(2:3)` is 2x1 so for datatype `array` the transpose would indeed not make sense as described here: http://stackoverflow.com/questions/11885503/numpy-transpose-not-giving-expected-result Perhaps using datatype `matrix` in numpy would make for a better translation. (Actually this is the word mentioned in the question) – Dennis Jaheruddin Nov 05 '13 at 17:07
  • @WarrenWeckesser I think you're right. I think that the transpose is unnecessary in Python, because array([1,2]) is the same as its transpose. I think that part of the code is unnecessary in my Python translation. In this case, I don't think it needs to be in matrix form per se. – J Jones Nov 05 '13 at 18:12

2 Answers2

5

use a.conj().transpose() for Matlab's transpose (a')

and a.transpose() for Matlab's non-conjugate transpose (a.' or transpose(a))

bla
  • 25,846
  • 10
  • 70
  • 101
  • A(3:2) is a single value, so I didn't think that transpose made sense. A = [ 0.08390008 -0.58820993 -0.57735027 ] Thanks! – J Jones Nov 04 '13 at 23:49
  • 2
    first it is `A(2:3)` in your question. second, this outputs two values `[A(2) A(3)]`. Third, the transpose is then `transpose([A(2) A(3)])` (i.e. a row vector becomes a column vector etc) – bla Nov 04 '13 at 23:59
  • Ok, I see. In Python indexing is not inclusive of the last element, and I didn't realize that this was not the case in matlab. Thanks. – J Jones Nov 05 '13 at 18:09
2

It's just to return the transpose of the matrix.

In python using numpy, you can do the following:

a.T
a.transpose()

Both will return the same result.