Could you, please, help me with operator overloading for multiplication of two different class pointers? I'd like to program this operation like in MATLAB, without extra subroutine.

Class definition:

class Vector
{
public:
double* V;
unsigned int Size;

Vector(unsigned int Elems, bool ZeroV = true)
{
Size = Elems;

if (Size < 1) return;

V = new double[Size];

if (ZeroV)
{
for (int i = 0; i < Size; i++)
V[i] = 0.0;
}
}

~Vector()
{
delete[] V;
}
};

class Matrix
{
public:
double** M;
unsigned int Rows;
unsigned int Cols;

Matrix(unsigned int Row, unsigned int Col, bool ZeroM = true)
{
Rows = Row; Cols = Col;

if (Rows < 1 || Cols < 1) return;

M = new double*[Rows];

for (int i = 0; i < Rows; i++)
M[i] = new double[Cols];

if (ZeroM)
{
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Cols; j++)
M[i][j] = 0.0;
}
}

~Matrix()
{
for (int i = 0; i < Rows; i++)
delete[] M[i];

delete[] M;
}
};

My attempts to code this:

Vector* operator* (const Matrix *M)
{
Vector* Result = new Vector(M->Cols);

if (this->Size != M->Rows)
return Result;

for (int i = 0; i < M->Cols; i++)
for (int j = 0; j < M->Rows; j++)
Result->V[i] += this->V[j] * M->M[j][i];

return Result;
}

main function:

void main()
{
Vector* V = new Vector(5, true);
Matrix* M = new Matrix(5, 2, true);

Vector* Vnew = V * M;
}
It's possible to call C/C++ in Matlab https://www.mathworks.com/help/simulink/slref/coder.ceval.html

I assume vector here means column vector. If so, multiplying a column vector of length 5 by a 5x2 matrix isn't possible (V * M, [5x1]*[5x2]). Can we see the Matlab code?

Anyway,
(1) use int main, not void main
(2) Don't dynamically allocate the Vector or Matrix objects themselves.

Prefer to do:
 ``1234`` ``````Matrix M = Matrix(2, 3); Vector V = Vector(3, true); Vector V_result = M * V;``````

and go from there. Of course you'll need to change the operator* to take in a const reference to a matrix instead of a const pointer.

Edit: Wait a sec what's your actual issue? What errors are you getting, runtime or compile time?
Last edited on
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````class Vector { public: double* V; unsigned int Size; Vector(unsigned int Elems, bool ZeroV = true) { Size = Elems; if (Size < 1) return; V = new double[Size]; if (ZeroV) { for (unsigned int i = 0; i < Size; i++) V[i] = 0.0; } } ~Vector() { delete[] V; } }; class Matrix { public: double** M; unsigned int Rows; unsigned int Cols; Matrix(unsigned int Row, unsigned int Col, bool ZeroM = true) { Rows = Row; Cols = Col; if (Rows < 1 || Cols < 1) return; M = new double*[Rows]; for (unsigned int i = 0; i < Rows; i++) M[i] = new double[Cols]; if (ZeroM) { for (unsigned int i = 0; i < Rows; i++) for (unsigned int j = 0; j < Cols; j++) M[i][j] = 0.0; } } ~Matrix() { for (unsigned int i = 0; i < Rows; i++) delete[] M[i]; delete[] M; } }; Vector operator*(const Matrix& mat, const Vector& vec) { Vector result(mat.Rows, true); for (unsigned int i = 0; i < mat.Rows; i++) for (unsigned int j = 0; j < mat.Cols; j++) result.V[i] += mat.M[i][j] * vec.V[j]; return result; } #include int main() { Matrix M = Matrix(2, 3); M.M[0][0] = 1; // row 0, column 0 M.M[0][1] = 2; M.M[0][2] = 3; M.M[1][0] = 4; // row 1, column 0 M.M[1][1] = 5; M.M[1][2] = 6; Vector V = Vector(3); V.V[0] = 7; V.V[1] = 9; V.V[2] = 11; Vector Vnew = M * V;; std::cout << Vnew.V[0] << '\n' << Vnew.V[1] << '\n'; }``````

Sloppy but here's an example of what I'm talking about, based on your code.
This might no be what you were looking for, so feel free to say so.

PS: To simplify construction, look into std::initializer_list
https://en.cppreference.com/w/cpp/utility/initializer_list
So that you could do something like `Matrix m = { {1, 2}, {3, 4 } };`
Last edited on
Topic archived. No new replies allowed.