Matrix transposition: Difference between revisions
Content added Content deleted
(→{{header|Go}}: improve transpose in place to working program, add library solution.) |
|||
Line 1,570: | Line 1,570: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<lang php>function transpose($m) { |
<lang php>function transpose($m) { |
||
if (count($m) == 0) // special case: empty matrix |
|||
return array(); |
|||
else if (count($m) == 1) // special case: row matrix |
|||
return array_chunk($m[0], 1); |
|||
// array_map(NULL, m[0], m[1], ..) |
// array_map(NULL, m[0], m[1], ..) |
||
array_unshift($m, NULL); // the original matrix is not modified because it was passed by value |
array_unshift($m, NULL); // the original matrix is not modified because it was passed by value |
||
return call_user_func_array('array_map', $m); |
return call_user_func_array('array_map', $m); |
||
}</lang> |
}</lang> |
||
Caveat: this will not work correctly for a row matrix (matrix with only 1 row); it should return a column matrix (matrix where each row has length 1), but simply returns the row itself, which is a 1-dimensional array, not a matrix. |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |