Phpml\Math\Matrix::transpose PHP Method

transpose() public method

public transpose ( ) : Matrix
return Matrix
    public function transpose()
    {
        $newMatrix = [];
        for ($i = 0; $i < $this->rows; ++$i) {
            for ($j = 0; $j < $this->columns; ++$j) {
                $newMatrix[$j][$i] = $this->matrix[$i][$j];
            }
        }
        return new self($newMatrix, false);
    }

Usage Example

Example #1
0
 public function testMatrixTranspose()
 {
     $matrix = new Matrix([[3, 3, 3], [4, 2, 1], [5, 6, 7]]);
     $transposedMatrix = [[3, 4, 5], [3, 2, 6], [3, 1, 7]];
     $this->assertEquals($transposedMatrix, $matrix->transpose()->toArray());
 }