Transpose of a non-square matrix (without using ndarray.transpose)

  • Python
  • Thread starter Wrichik Basu
  • Start date
  • #1
1,438
1,300

Main Question or Discussion Point

While the prefix of the thread is Python, this could be easily generalised to any language.

It is absolutely not the first time I am working with an array, but definitely the first time I am facing the task of defining the transpose of a non-square matrix. I have worked so much with arrays in Java, but unfortunately this simple yet tricky problem never came to my mind.

Numpy has a transpose function. But without using that, how can you define the transpose of a general m x n matrix, where ##m \neq n##?

Addition:
When I was learning Java, whenever I used any inbuilt function, I made it a habit to read up that documentation of that class, and then the class itself (from NetBeans, I could open the inbuilt classes). Similarly, in PyCharm, I navigated to the Numpy module, and opened the _multiarray_umath.py script. For the transpose function, I found this:
Python:
    def transpose(self, *axes): # real signature unknown; restored from __doc__
        """
        a.transpose(*axes)
        
            Returns a view of the array with axes transposed.
        
            For a 1-D array this has no effect, as a transposed vector is simply the
            same vector. To convert a 1-D array into a 2D column vector, an additional
            dimension must be added. `np.atleast2d(a).T` achieves this, as does
            `a[:, np.newaxis]`.
            For a 2-D array, this is a standard matrix transpose.
            For an n-D array, if axes are given, their order indicates how the
            axes are permuted (see Examples). If axes are not provided and
            ``a.shape = (i[0], i[1], ... i[n-2], i[n-1])``, then
            ``a.transpose().shape = (i[n-1], i[n-2], ... i[1], i[0])``.
        
            Parameters
            ----------
            axes : None, tuple of ints, or `n` ints
        
             * None or no argument: reverses the order of the axes.
        
             * tuple of ints: `i` in the `j`-th place in the tuple means `a`'s
               `i`-th axis becomes `a.transpose()`'s `j`-th axis.
        
             * `n` ints: same as an n-tuple of the same ints (this form is
               intended simply as a "convenience" alternative to the tuple form)
        
            Returns
            -------
            out : ndarray
                View of `a`, with axes suitably permuted.
        
            See Also
            --------
            ndarray.T : Array property returning the array transposed.
            ndarray.reshape : Give a new shape to an array without changing its data.
        
            Examples
            --------
            >>> a = np.array([[1, 2], [3, 4]])
            >>> a
            array([[1, 2],
                   [3, 4]])
            >>> a.transpose()
            array([[1, 3],
                   [2, 4]])
            >>> a.transpose((1, 0))
            array([[1, 3],
                   [2, 4]])
            >>> a.transpose(1, 0)
            array([[1, 3],
                   [2, 4]])
        """
        pass
As seen above, the only statement in the function is pass.

How does this function work? Or am I looking up the wrong function? (I have Python 3.7.4)
 
Last edited:

Answers and Replies

  • #2
34,467
10,586
As the function does something this can't be the function you actually call.
What it does is quite clever, however. It doesn't change anything in the array memory, it just swaps the way the array is addressed. Here is a description. It also has a link to a transpose code that does something.
 
  • #3
1,438
1,300
As the function does something this can't be the function you actually call.
What it does is quite clever, however. It doesn't change anything in the array memory, it just swaps the way the array is addressed. Here is a description. It also has a link to a transpose code that does something.
That is an interesting way to do the job, and it answers the second part of my question. I also found that the numpy.swapaxes() function does the same thing. But any idea how to do the same job without using any function?

The problem basically arises because for non-square matrices, we can no longer write ##a^T_{ij} \ = \ a_{ji}##.
 
  • #4
marcusl
Science Advisor
Gold Member
2,725
386
The problem basically arises because for non-square matrices, we can no longer write ##a^T_{ij} \ = \ a_{ji}##.
Why do you say that?
 
  • #5
1,438
1,300
Why do you say that?
Consider the following code:
Python:
>>>a = np.array([[2, 3, 4], [5, 6, 7]])
>>>m = np.size(a,0)
>>>n = np.size(a,1)
>>>b = np.empty((n,m))
>>>for i in range(0,m):
...    for j in range(0,n):
...        b[i][j] = a[j][i]
...
This returns the following error:
1567089496443.png

But this runs properly once ##a## is a square matrix.
 
  • #6
Ibix
Science Advisor
Insights Author
6,605
5,359
I think you've got your axes confused. Have you tried swapping i and j on both sides of the assignment in line 7?
 
  • #7
marcusl
Science Advisor
Gold Member
2,725
386
Ibix beat me to it...
 
  • #8
1,438
1,300
I think you've got your axes confused. Have you tried swapping i and j on both sides of the assignment in line 7?
Maybe I had lost my mind, :headbang: yes that solves the problem.
 
  • #9
Ibix
Science Advisor
Insights Author
6,605
5,359
Maybe I had lost my mind, :headbang: yes that solves the problem.
Don't worry about making stupid mistakes - you'll never stop making them. Worry about preparations to catch them when they happen - good debugging, testing and general QA. It's the mistakes of general understanding you can fix.

That said, one thing that could have helped you here is to name your variables in a different way. For example, call m and n size0 and size1, and i and j i0 and i1. Then when you wrote a[i1][i0] you'd probably have realised that was wrong. It's not always the right thing to do since sometimes you want to follow the conventions of wherever your maths comes from, but it's worth considering.
 
  • Like
Likes FactChecker, marcusl and Wrichik Basu
  • #10
1,785
139
Transpose of a matrix can be find easily by using zip function. For instance if you have 2x2 matrix you can use zip like this

Python:
xmatrix = [[1, 2],
           [3, 4]]

print(list(zip(xmatrix[0], xmatrix[1])))
and the result would be
Code:
[(1, 3), (2, 4)]
or for ##m≠n##
Python:
xmatrix = [[1, 2],
           [3, 4],
           [2, 7]]

print(list(zip(xmatrix[0], xmatrix[1], xmatrix[2])))
and it prints

Code:
[(1, 3, 2), (2, 4, 7)]
And finally

Python:
xmatrix = [[1, 2, 7, 8],
           [3, 4, 9, 0]]

print(list(zip(xmatrix[0], xmatrix[1])))
Code:
[(1, 3), (2, 4), (7, 9), (8, 0)]
 
  • Like
Likes Wrichik Basu
  • #11
34,467
10,586
Your code depends on the size of the matrix.
 
  • #12
1,785
139
Your code depends on the size of the matrix.
Well yes. But I think in most cases we know the size of it. I agree that, in general, it might not be very useful.

If we have different matrix sizes in our data, we can do it like this

Python:
xmatrix = [[1, 2, 7, 8],[3, 4, 9, 0]]
xmatrix1 = [[4,5],[1,2],[0,-10]]

def transpose(xmatrix):
  if len(xmatrix) == 2:
    return list(zip(xmatrix[0], xmatrix[1]))
  elif len(xmatrix) == 3:
    return list(zip(xmatrix[0], xmatrix[1], xmatrix[2]))

print(transpose(xmatrix))
print(transpose(xmatrix1))
I would not prefer this one if I had a huge amount of matrix data. But If I had couple matrices I would prefer zip.
 
  • #13
1,438
1,300
I would not prefer this one if I had a huge amount of matrix data.
I always prefer to work with Numpy matrices rather than the lists. Never having used lists in Java till now, I can most probably work without them, unless the data becomes huge and I would like to save memory space by creating variable-size lists rather than arrays with fixed size. The additional advantage is that I can use the functions of Numpy when I need them. For a large program where transpose is maybe just 0.01% of the total code, I would prefer to use ndarray.transpose() rather than create my own functions.
 
  • Like
Likes Arman777
  • #15
wle
312
137
Transpose of a matrix can be find easily by using zip function. For instance if you have 2x2 matrix you can use zip like this

Python:
xmatrix = [[1, 2],
           [3, 4]]

print(list(zip(xmatrix[0], xmatrix[1])))
Better, using argument unpacking:
Python:
list(zip(*xmatrix))
This works for a matrix (list of lists) of any size.
 
  • Like
Likes Wrichik Basu, mfb and Arman777
  • #16
1,785
139
Thats great thanks a lot
 

Related Threads on Transpose of a non-square matrix (without using ndarray.transpose)

Replies
2
Views
27K
  • Last Post
Replies
4
Views
6K
Replies
4
Views
3K
Replies
6
Views
844
Replies
6
Views
2K
Replies
12
Views
772
  • Last Post
Replies
0
Views
1K
Replies
4
Views
699
  • Last Post
Replies
7
Views
3K
Top