0

I import a matrix from an Excel worksheet. I need the product (not the inner product) from the last row of that matrix times a diagonal matrix. The result should be a new matrix with the diagonal matrix's dimension, but I get instead a 1 row (and n columns) result. However, when I introduce the data of the particular row manually as a vector (c() function), I get the required result.

I have tried multiplying the specific row by the diagonal matrix and vice versa and always get the same (one row) result.

  • 2
    Welcome to Stack Overflow. You'll get better answers if you [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including the input datasets and desired output in a plain text format - for example the output from `dput(yourdata)`, if that is not too large. Also include all the code required to reproduce the issue and any error messages. – neilfws Jun 06 '23 at 01:33
  • 1
    @GregorThomas that will be an error. I believe op is trying to carry out elementwise multiplication instead of matrix multiplication. But since OP did not give an example, its difficult to tell. Although you cannot directly transpose the row before multiplying as that would make the matrices to be incompatible – Onyambu Jun 06 '23 at 02:49
  • 1
    Ah, misunderstood. You could try `diag(last_row * diag(your_diagonal_matrix))` – Gregor Thomas Jun 06 '23 at 03:27
  • Thanks very much Onyambu and Gregor. My intention certainly is an elementwise multiplication and with Gregor's suggestion, I found the solution: diag(last_row). There was no need to multiply it by the diagonal (don't know why). Thanks again – Juan Carlos Guevara Jun 06 '23 at 22:45

0 Answers0