Flip (Transpose) the Rows and Columns of a 2D Array Without Changing the Number of Columns

Flip (transpose) the rows and columns of a 2D array without changing the number of columns

Just walk the array in the correct order. Assuming you have relatively small arrays, the easiest solution is just to create a brand new array during that walk.

A solution will be of the form:

$rows = count($arr);
$ridx = 0;
$cidx = 0;

$out = array();

foreach($arr as $rowidx => $row){
foreach($row as $colidx => $val){
$out[$ridx][$cidx] = $val;
$ridx++;
if($ridx >= $rows){
$cidx++;
$ridx = 0;
}
}
}

How to transpose an array but without changing the number of rows and columns in c?

The matrix

    1,4,7,10,
2,5,8,11,
3,6,9,12

can be written as

0*3+1, 1*3+1, 2*3+1, 3*3+1
0*3+2, 1*3+2, 2*3+2, 3*3+2
0*3+3, 1*3+3, 2*3+3, 3*3+3

On the other hand, the matrix

    1,2,3,4,
5,6,7,8,
9,10,11,12

can be written as

0*4+1, 0*4+2, 0*4+3, 0*4+4
1*4+1, 1*4+2, 1*4+3, 1*4+4
2*4+1, 2*4+2, 2*4+3, 2*4+4

Now it seems clearer. Then calculate the source and desitination coordinates and do assignments.

for(i=0;i<4*3;i++)
{
int src_y=i%3, src_x=i/3, dst_y=i/4, dst_x=i%4;
copy[dst_y][dst_x]=arr[src_y][src_x];
}

How to swap rows and columns of a 2d array?

You could iterate over the rows and columns and assign each element [i,j] to the transposed [j,i]:

/**
* Transposses a matrix.
* Assumption: mat is a non-empty matrix. i.e.:
* 1. mat != null
* 2. mat.length > 0
* 3. For every i, mat[i].length are equal and mat[i].length > 0
*/
public static int[][] transpose(int[][] mat) {
int[][] result = new int[mat[0].length][mat.length];
for (int i = 0; i < mat.length; ++i) {
for (int j = 0; j < mat[0].length; ++j) {
result[j][i] = mat[i][j];
}
}
return result;
}

Transpose rows and columns in a 2D array

This should give you what you need.

function transpose($array_one) {
$array_two = [];
foreach ($array_one as $key => $item) {
foreach ($item as $subkey => $subitem) {
$array_two[$subkey][$key] = $subitem;
}
}
return $array_two;
}

Then just pipe your existing array into the function and render the resulting array.

Transposing a 2D-array in JavaScript

output = array[0].map((_, colIndex) => array.map(row => row[colIndex]));

map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.

callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed. [source]

Transpose a two dimensional Array with one row and many columns into a two dimensional array with many rows and one column

I guess your goal as follows.

  • From var dataArr = shrinkLog.getRange(2,3,1,shrinkLog.getLastColumn()).getValues(), you want to retrieve the result from [[data1, data2, data3,,,]] to [[data1], [data2], [data3],,,]].

In your case, shrinkLog.getRange(2,3,1,shrinkLog.getLastColumn()).getValues() returns [[data1, data2, data3,,,]]. So when your goal is achieved with map, the modified script is as follows.

Sample script:

var dataArr = shrinkLog.getRange(2,3,1,shrinkLog.getLastColumn()).getValues()[0].map(c => [c]);

Note:

  • In this case, please enable V8 runtime.

Reference:

  • map()

Trying to flip columns and rows in an array in python. I can print them, but not sure how make them into a new array

You were pretty close with your printing, you just need to make a new list,

new = []
for i in range(len(testboard)):
new.append([])
for j in range(len(testboard)):
new[i].append(testboard[j][i])


Related Topics



Leave a reply



Submit