From 2878ba6a02100dd03dc005229fed50089b42f970 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Thu, 3 Feb 2022 19:59:20 +0000 Subject: [PATCH] Added transpose --- .../java/com/mattrixwv/matrix/IntegerMatrix.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java index 02e6872..a9cde9a 100644 --- a/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java @@ -478,6 +478,20 @@ public class IntegerMatrix{ } //Complex operations + public IntegerMatrix transpose(){ + //Create a new grid + int[][] newGrid = new int[getNumCols()][getNumRows()]; + + //Traverse every element in the existing grid and add each column to the new grid as a row + for(int col = 0;col < getNumCols();++col){ + for(int row = 0;row < getNumRows();++row){ + newGrid[col][row] = grid[row][col]; + } + } + + //Return the new matrix + return new IntegerMatrix(newGrid); + } //Object funtions @Override