Skip to content

Commit 2ae4930

Browse files
committed
Updated Matrix.java, added Transpose of a Matrix
1 parent 05efd17 commit 2ae4930

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Data Structures/Matrix/Matrix.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,20 @@ public String toString() {
221221

222222
return str;
223223
}
224+
225+
/**
226+
* Returns transposed matrix of this matrix.
227+
*
228+
* @return transposed Matrix.
229+
*/
230+
public Matrix transpose() {
231+
232+
int[][] newData = new int[this.data[0].length][this.data.length];
233+
234+
for (int i = 0; i < this.getColumns(); ++i)
235+
for(int j = 0; j < this.getRows(); ++j)
236+
newData[i][j] = this.data[j][i];
237+
238+
return new Matrix(newData);
239+
}
224240
}

0 commit comments

Comments
 (0)