Multiply a square matrix with only the diagonal and antidiagonal elements different from zero in vector So I have a square matrix with the diagonal and antidiagonal elements different from zero in a vector
vector&lt;int&gt; matrix = {1,2,3,4,5,6,7,8};

The matrix:
1 0 0 5
0 2 6 0
0 7 3 0
8 0 0 4

I try to multiply this with another matrix
xmatrix is the class for matrices
I tried like this:
void xmatrix::operator*(const xmatrix&amp; mat)
{
    if(matrixSize != mat.getmatrixSize()) throw &quot;Diff size&quot;;
    int x;
    for(int i = 0;i &lt; matrixSize;i++){
        for(int j = 0;j &lt; matrixSize;j++){
            x = 0;
            for(int k = 0;k &lt; matrixSize;k++){
                x += getElement(i,k) * mat.getElement(k,j);
            }
            setElementMul(i,j,x);
        }
    }
}

The var and func names clearly says it all
ATM this code works fine with the first half of the matrix (multiply with itself)
41 0 0 225
0 46 294 0
0 35 219 0
40 0 0 216

what other ways can work fine or how should i edit this code
I believe your issue is that you're altering your matrix (the multiplicand) as you are performing the multiplication. This is why part of the result is correct while the other part is incorrect. For example, the top-right cell (which should be 25) is evaluated from \$ 41*5+5*4=225\$ where the 41 (which should be 1) is used because you've prematurely set that cell from an earlier iteration.
A simple way to fix this could be to use an intermediary matrix or buffer during the calculation. However, if your xmatrix class represents a matrix that can only be diagonal, anti-diagonal, or both, then you can use the properties of diagonal matrices to optimize and simplify your class.
If this is indeed the case, then you can simply represent the entire matrix by its diagonal and anti-diagonal components:
vector&lt;int&gt; diagonal;
vector&lt;int&gt; antidiag;

For the anti-diagonal component, I'll refer to your example and use a top-right to bottom-left orientation (so the top-right cell will be first and the bottom-left cell will be last).
We can now optimize the multiplication operation by noting a few properties of diagonal matrices. Consider a diagonal matrix \$ A_x \$ and an anti-diagonal matrix \$ A_y \$, both of size \$ n \$. Let these be the anti-diagonal and diagonal components of an \$ n*n \$ matrix \$ A \$. We can then represent \$ A \$ such that \$ A = A_x+A_y \$. However, this representation requires special care when \$ n \$ is odd because of the common middle element. When such a case occurs, we'll absorb the middle element into the diagonal component and zero out the middle element in the anti-diagonal component. So now we have our multiplicand matrix \$ A \$, we use a similar notation to represent the multiplier matrix \$ B \$ such that \$ B = B_x+B_y \$. Using a few properties of matrices, we can show that the multiplication of these matrices is a sum of diagonal and anti-diagonal components:
$$
AB=(A_x+A_y)(B_x+B_y)=A_xB_x+A_xB_y+A_yB_x+A_yB_y\\=(A_xB_x+A_yB_y)+(A_xB_y+A_yB_x)\\=(\text{[diagonal]})+(\text{[anti-diagonal]})
$$
Now that we've worked through the math, we can use this result to implement an optimized multiplication algorithm with a time complexity of \$ O(n) \$.
#include &lt;iostream&gt;
#include &lt;vector&gt;

class XMatrix
{
public:
    XMatrix(const std::vector&lt;int&gt;&amp; diagonal, const std::vector&lt;int&gt;&amp; antidiag)
     : diagonal(diagonal), antidiag(antidiag), size(diagonal.size())
    {
        if (diagonal.size() != antidiag.size())
            throw;

        if (this-&gt;size % 2 != 0) {
            this-&gt;antidiag[this-&gt;size / 2] = 0;
        }
    }
/*
...
*/

    XMatrix operator*(const XMatrix&amp; m)
    {
        if (this-&gt;size != m.size)
            throw;

        std::vector&lt;int&gt; result_diagonal(this-&gt;size);
        std::vector&lt;int&gt; result_antidiag(this-&gt;size);

        for (int i = 0; i &lt; this-&gt;size; ++i) {
            result_diagonal[i]
             = this-&gt;diagonal[i] * m.diagonal[i] +
               this-&gt;antidiag[i] * m.antidiag[m.size-i-1];
            result_antidiag[i]
             = this-&gt;diagonal[i] * m.antidiag[i] +
               this-&gt;antidiag[i] * m.diagonal[m.size-i-1];
        }

        return XMatrix(result_diagonal, result_antidiag);
    }

    void print() const
    {
        for (int row = 0; row &lt; this-&gt;size; ++row) {
            for (int col = 0; col &lt; this-&gt;size; ++col) {
                std::cout &lt;&lt; '\t';
                if (row == col)
                    std::cout &lt;&lt; this-&gt;diagonal[row];
                else if (row == (this-&gt;size - col - 1))
                    std::cout &lt;&lt; this-&gt;antidiag[row];
                else
                    std::cout &lt;&lt; '0';
            }
            std::cout &lt;&lt; '\n';
        }
    }

private:
    int size;
    std::vector&lt;int&gt; diagonal;
    std::vector&lt;int&gt; antidiag;
};

int main(int argc, char *argv[])
{
    XMatrix matrix_A({1,2,3,4},{5,6,7,8});
    XMatrix matrix_B = matrix_A * matrix_A;
    matrix_B.print();

    return 0;
}

I've altered some things compared to your original code for the purpose of illustrating everything within this response. This may be a bit more than you were asking for regarding why your matrix multiplication was only half working (if that's all you were concerned about then feel free to disregard everything after the first two paragraphs), but in any case I hope I was able to help.