Questions tagged [transpose]

Transposition is an operation that applies to matrices wherein the rows and columns are swapped

The transpose of a matrix A is written as A'. The transpose of matrix A is another matrix B in which the element in the ith row and the jth column of A is in the jth row and the ith column of B. For example:

Suppose A =

[[11, 12, 13],
 [21, 22, 23],
 [31, 32, 33]]

Then, A' =

[[11, 21, 31],
 [12, 22, 32],
 [13, 23, 33]]

In scientific software for statistical computing and graphics, function t transposes a matrix.

3257 questions
601
votes
14 answers

Transpose/Unzip Function (inverse of zip)?

I have a list of 2-item tuples and I'd like to convert them to 2 lists where the first contains the first item in each tuple and the second list holds the second item. For example: original = [('a', 1), ('b', 2), ('c', 3), ('d', 4)] # and I want to…
Cristian
  • 42,563
  • 25
  • 88
  • 99
370
votes
14 answers

Transpose list of lists

Let's take: l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] The result I'm looking for is r = [[1, 4, 7], [2, 5, 8], [3, 6, 9]] and not r = [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
titus
  • 5,512
  • 7
  • 25
  • 39
306
votes
24 answers

Javascript equivalent of Python's zip function

Is there a javascript equivalent of Python's zip function? That is, given multiple arrays of equal lengths create an array of pairs. For instance, if I have three arrays that look like this: var array1 = [1, 2, 3]; var array2 = ['a','b','c']; var…
pq.
  • 3,415
  • 2
  • 18
  • 16
274
votes
15 answers

Transposing a 1D NumPy array

I use Python and NumPy and have some problems with "transpose": import numpy as np a = np.array([5,4]) print(a) print(a.T) Invoking a.T is not transposing the array. If a is for example [[],[]] then it transposes correctly, but I need the…
thaking
  • 3,495
  • 8
  • 27
  • 33
248
votes
25 answers

Transposing a 2D-array in JavaScript

I've got an array of arrays, something like: [ [1,2,3], [1,2,3], [1,2,3], ] I would like to transpose it to get the following array: [ [1,1,1], [2,2,2], [3,3,3], ] It's not difficult to programmatically do so using…
ckersch
  • 7,507
  • 2
  • 37
  • 44
138
votes
33 answers

An efficient way to transpose a file in Bash

I have a huge tab-separated file formatted like this X column1 column2 column3 row1 0 1 2 row2 3 4 5 row3 6 7 8 row4 9 10 11 I would like to transpose it in an efficient way using only bash commands (I could write a ten or so lines Perl script to…
Federico Giorgi
  • 10,495
  • 9
  • 42
  • 56
119
votes
6 answers

How does NumPy's transpose() method permute the axes of an array?

In [28]: arr = np.arange(16).reshape((2, 2, 4)) In [29]: arr Out[29]: array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7]], [[ 8, 9, 10, 11], [12, 13, 14, 15]]]) In [32]: arr.transpose((1, 0, 2)) Out[32]: array([[[ 0, 1, 2, …
Frank Hu
  • 1,195
  • 2
  • 9
  • 6
91
votes
12 answers

What is the fastest way to transpose a matrix in C++?

I have a matrix (relatively big) that I need to transpose. For example assume that my matrix is a b c d e f g h i j k l m n o p q r I want the result be as follows: a g m b h n c I o d j p e k q f l r What is the fastest way to do this?
mans
  • 17,104
  • 45
  • 172
  • 321
75
votes
14 answers

Is there a php function like python's zip?

Python has a nice zip() function. Is there a PHP equivalent?
Craig
  • 7,471
  • 4
  • 29
  • 46
69
votes
1 answer

Transpose latest rows per user to columns

I have the following table, which gives multiple email addresses for each user. I need to flatten this out to columns on a user query. To give me the "newest" 3 email addresses based on the creation date.…
dacology
  • 954
  • 2
  • 7
  • 13
59
votes
5 answers

Rotate a Matrix in R by 90 degrees clockwise

I have a matrix in R like this: |1|2|3| |1|2|3| |1|2|3| Is there an easy way to rotate the entire matrix by 90 degrees clockwise to get these results? |1|1|1| |2|2|2| |3|3|3| and again rotating 90 degrees: |3|2|1| |3|2|1| |3|2|1| ?
Johannes
  • 829
  • 2
  • 8
  • 10
55
votes
2 answers

How to pivot a dataframe in Pandas?

I have a table in csv format that looks like this. I would like to transpose the table so that the values in the indicator name column are the new columns, Indicator Country Year Value 1 Angola 2005 6 2…
bjurstrs
  • 949
  • 2
  • 7
  • 17
48
votes
9 answers

Transpose column to row with Spark

I'm trying to transpose some columns of my table to row. I'm using Python and Spark 1.5.0. Here is my initial table: +-----+-----+-----+-------+ | A |col_1|col_2|col_...| +-----+-------------------+ | 1 | 0.0| 0.6| ... | | 2 | 0.6| 0.7|…
Raouf
  • 989
  • 2
  • 11
  • 15
39
votes
5 answers

Transposing a dataframe maintaining the first column as heading

I have a big dataframe, but small example would be like this: mydf <- data.frame(A = c(letters[1:10]), M1 = c(11:20), M2 = c(31:40), M3 = c(41:50)) I want to transpose the dataframe and maintain the column 1 (A) as column heading ( letter[1:10]) as…
jon
  • 11,186
  • 19
  • 80
  • 132
35
votes
5 answers

Matrix expression causes error "requires numeric/complex matrix/vector arguments"?

ma=diag(3)+t(da)%*%da R Code above, error message as follows: Error in t(da) %*% da : requires numeric/complex matrix/vector arguments da is a matrix, looks as following: V45 V46 V47 V48 V49 V50 V51…
user3505808
  • 405
  • 2
  • 5
  • 6
1
2 3
99 100