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
11
votes
8 answers

How would you transpose a binary matrix?

I have binary matrices in C++ that I repesent with a vector of 8-bit values. For example, the following matrix: 1 0 1 0 1 0 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 is represented as: const uint8_t matrix[] = { 0b01010101, 0b00110011, …
Venemo
  • 18,515
  • 13
  • 84
  • 125
11
votes
3 answers

How to I invert a 2d list in python

I have a 2d list like this: 1 2 3 4 5 6 and I want to make this: 1 4 2 5 3 6 I've tried to do a for loop and switch each value but I keep getting an index out of bound error. Here's what I have: for i in results: …
DannyD
  • 2,732
  • 16
  • 51
  • 73
11
votes
5 answers

Is there a safe way in Scala to transpose a List of unequal-length Lists?

Given the following List: val l = List(List(1, 2, 3), List(4, 5), List(6, 7, 8)) If I try to transpose it, Scala will throw the following error: scala> List.transpose(l) java.util.NoSuchElementException: head of empty list at…
pr1001
  • 21,727
  • 17
  • 79
  • 125
11
votes
1 answer

Get the first column of a matrix represented by a vector of vectors

Suppose I'm representing a matrix foo of values using std::vector: int rows = 5; int cols = 10; auto foo = vector>(rows, vector(cols)); Is there a cleverly simple way for me to get a vector of size rows that contains…
Alan Turing
  • 12,223
  • 16
  • 74
  • 116
10
votes
1 answer

Why is performing matrix multiplication on a pre-transposed matrix faster than on a non-transposed matrix?

Consider the following code in Python, where multiplying a pre-transposed matrix yields faster execution time compared to multiplying a non-transposed matrix: import numpy as np import time # Generate random matrix matrix_size = 1000 matrix =…
zoldxk
  • 2,632
  • 1
  • 7
  • 29
10
votes
3 answers

Python list transpose and fill

I have a list of lists such that the length of each inner list is either 1 or n (assume n > 1). >>> uneven = [[1], [47, 17, 2, 3], [3], [12, 5, 75, 33]] I want to transpose the list, but instead of truncating the longer list (as with zip) or…
kojiro
  • 74,557
  • 19
  • 143
  • 201
9
votes
7 answers

What is the fastest way to transpose the bits in an 8x8 block on bits?

I'm not sure the exact term for what I'm trying to do. I have an 8x8 block of bits stored in 8 bytes, each byte stores one row. When I'm finished, I'd like each byte to store one column. For example, when I'm finished: Byte0out = Byte0inBit0 +…
Roland Rabien
  • 8,750
  • 7
  • 50
  • 67
9
votes
3 answers

How to transpose a table in SQLite?

Hello so I have a table as such in SQlite: User | Group | Role John Smith | A | admin John Smith | B | user Jane Doe | A | user Jane Doe | B | limit Jane Doe | C | admin Jack Brown | A …
harcot
  • 317
  • 1
  • 4
  • 7
9
votes
8 answers

How to transpose a matrix in prolog

How can I transpose a list like [[1,2,3][4,5,6][6,7,8]] to [[1,4,6],[2,7,8],[3,6,9]]? To depict it: I'd like to flip the matrix 90 degree to the left. How can I do that?
cody
  • 6,389
  • 15
  • 52
  • 77
9
votes
1 answer

Transpose the data in a column every nth rows in PANDAS

For a research project, I need to process every individual's information from the website into an excel file. I have copied and pasted everything I need from the website onto a single column in an excel file, and I loaded that file using PANDAS.…
Molly Zhao
  • 145
  • 2
  • 10
9
votes
3 answers

Transposing (pivoting) a dict of lists in python

I have a dictionary that looks like: {'x': [1, 2, 3], 'y': [4, 5, 6]} I want to transform it to the following format: [{'x': 1, 'y': 4}, {'x': 2, 'y': 5}, {'x': 3, 'y': 6}] I can do it by explicit loops but is there a good pythonic way to do…
Bilentor
  • 486
  • 1
  • 5
  • 13
9
votes
4 answers

Transpose a matrix in racket (list of lists

I got a list of lists in racket and have to transpose them. (: transpose ((list-of(list-of %a)) -> (list-of (list-of %a)))) (check-expect (transpose (list (list 1 2 3) (list 4 5 6))) (list (list 1 4) …
b4shyou
  • 91
  • 1
  • 1
  • 3
9
votes
1 answer

Why does Ruby have zip and transpose when they do the same thing?

They seem to do the same thing. g = [{ a: "A" }, { b: "B" }] r = [{ x: "X" }, { y: "Y" }] g.zip(r) # => [[{:a=>"A"}, {:x=>"X"}], [{:b=>"B"}, {:y=>"Y"}]] [g,r].transpose # => [[{:a=>"A"}, {:x=>"X"}], [{:b=>"B"}, {:y=>"Y"}]] Why have both…
Jumbalaya Wanton
  • 1,601
  • 1
  • 25
  • 47
9
votes
3 answers

How to transpose object in underscorejs

In JavaScript I am trying to convert an array of objects with similar keys: [{'a':1,'b':2}, {'a':3,'b':4}, {'a':5,'b':6,'c':7}] to an object with an array of values for each key: {'a':[1,3,5], 'b':[2,4,6], 'c':[7]}; using underscore.js 1.4.2. I…
Mark C
  • 1,314
  • 1
  • 12
  • 19
9
votes
1 answer

Transposing arbitrary collection-of-collections in Scala

I have to often transpose a "rectangular" collection-of-collections in Scala, e.g.: a list of maps, a map of lists, a map of maps, a set of lists, a map of sets etc. Since collections can be uniformly viewed as a mapping from a specific domain to a…