So I'm working on a small math library for fun and I'm having a little bit of an issue with this vector. This method is for transposing a matrix, swapping rows for columns and vice versa. It performs this correctly. However, there's something strange going on. From my input of a 3 row x 2 column matrix, the transposition should come back as 2x3, which it does. The problem is the vector is coming back as a size of 4 instead of 2, despite being set to 2 in the method. I've tested it at various points leading up to and after. The size of the vector only changes to 4 once it has called #transpose. It's prepending two vectors of size 0 before the answer (resulting matrix vector). Any ideas as to how? I'm not familiar enough with c++ to know of any quirks.
Matrix Matrix::transpose() {
vector<vector<float>> result;
result.resize(columns);
for (int col = 0; col < columns; col++) {
vector<float> data;
data.resize(rows);
for (int row = 0; row < rows; row++) {
data[row] = vec[row][col];
}
result.push_back(data);
}
return Matrix(result);
}