transpose

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

二次信任 提交于 2019-11-26 01:44:56
问题 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? 回答1: This is a good question. There are many reason you would want to actually transpose the matrix in memory rather than just swap coordinates, e.g. in matrix multiplication and Gaussian smearing. First let me list one of the functions I use for the transpose

Transpose column to row with Spark

对着背影说爱祢 提交于 2019-11-26 00:33:42
问题 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| ... | | 3 | 0.5| 0.9| ... | | ...| ...| ...| ... | I would like to have somthing like this: +-----+--------+-----------+ | A | col_id | col_value | +-----+--------+-----------+ | 1 | col_1| 0.0| | 1 | col_2| 0.6| | ...| ...| ...| | 2 | col_1| 0.6| | 2 | col

Javascript equivalent of Python's zip function

旧城冷巷雨未停 提交于 2019-11-25 23:57:23
问题 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 array3 = [4, 5, 6]; The output array should be: var output array:[[1,\'a\',4], [2,\'b\',5], [3,\'c\',6]] 回答1: 2016 update: Here's a snazzier Ecmascript 6 version: zip= rows=>rows[0].map((_,c)=>rows.map(row=>row[c])) Illustration equiv. to

An efficient way to transpose a file in Bash

流过昼夜 提交于 2019-11-25 22:39:56
问题 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 do that, but it should be slower to execute than the native bash functions). So the output should look like X row1 row2 row3 row4 column1 0 3 6 9 column2 1 4 7 10 column3 2 5 8 11 I thought of a solution like this cols=`head -n 1 input | wc -w` for (

Transpose list of lists

那年仲夏 提交于 2019-11-25 22:28:57
问题 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)] Much appreciated 回答1: How about map(list, zip(*l)) --> [[1, 4, 7], [2, 5, 8], [3, 6, 9]] For python 3.x users can use list(map(list, zip(*l))) 回答2: One way to do it is with the NumPy transpose. For a list, a: >>> import numpy as np >>> np.array(a).T.tolist() [[1, 4, 7], [2, 5, 8], [3, 6, 9]] Or another one without zip: >>> map

Transpose/Unzip Function (inverse of zip)?

我们两清 提交于 2019-11-25 21:49:55
问题 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 become... result = ([\'a\', \'b\', \'c\', \'d\'], [1, 2, 3, 4]) Is there a builtin function that does that? 回答1: zip is its own inverse! Provided you use the special * operator. >>> zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) [('a', 'b'

Transpose / reshape dataframe without “timevar” from long to wide format

自古美人都是妖i 提交于 2019-11-25 21:38:55
问题 I have a data frame that follows the below long Pattern: Name MedName Name1 atenolol 25mg Name1 aspirin 81mg Name1 sildenafil 100mg Name2 atenolol 50mg Name2 enalapril 20mg And would like to get below (I do not care if I can get the columns to be named this way, just want the data in this format): Name medication1 medication2 medication3 Name1 atenolol 25mg aspirin 81mg sildenafil 100mg Name2 atenolol 50mg enalapril 20mg NA Through this very site I have become familiarish with the reshape