0

If I have a matrixA that is 1x5 with all values = 1.0, and I want to resize it to 1x8 by having other elements as 0's, how do I do it?

Matrix<double> A = Matrix<double>.Build.Dense(1, 5, 1.0);

In other words, is it possible to multiple or carry out any operations on 2 different size matrices ?

opto abhi
  • 317
  • 1
  • 3
  • 13

2 Answers2

1

If you're talking about a 1 by X, it's a Vector. So one option is to make a second vector (ie, 1x8), and then use the

void CopySubVectorTo(Vector<T> destination, int sourceIndex, int targetIndex, int count)

to copy the non-zero elements of the 1X5 to the larger vector, or

void CopyTo(Vector<T> target)
J. McCabe
  • 115
  • 2
  • 9
  • Not necessarily a vector, consider a 2x2 to be expanded to 3x3 where the extra row and column elementsof 3x3 are 0's – opto abhi Nov 16 '16 at 19:59
  • The point is the same. You can make a second, larger matrix and copy the non-zero values from the smaller to the larger. – J. McCabe Nov 16 '16 at 20:04
  • doesnt work with MathNet. I have already tried that, the error is matrix dimensions do not agree. – opto abhi Nov 16 '16 at 20:06
  • If you're trying to do this to a matrix, not a vector, try – J. McCabe Nov 16 '16 at 20:11
  • void SetSubMatrix(int rowIndex, int columnIndex, Matrix subMatrix) Copies the values of a given matrix into a region in this matrix. – J. McCabe Nov 16 '16 at 20:12
1

To do this in a matrix, rather than the vector you originally described, here's an example of SetSubMatrix:

    Matrix<double> SubMatrix = Matrix<double>.Build.Dense(2, 2, 0.186);
    Matrix<double> BigMatrix = Matrix<double>.Build.Dense(3, 3);
    BigMatrix.SetSubMatrix(1, 1, SubMatrix);

Note you can place the submatrix anywhere inside the larger matrix. And here's the output where I placed the submatrix at (1,1) in the BigMatrix. And yes, it does work in MathNet if you know what function to use:

enter image description here

and here it's placed at (0,0):

enter image description here

J. McCabe
  • 115
  • 2
  • 9
  • what library are you using ? Is this Accord.NET ? – opto abhi Nov 17 '16 at 14:26
  • Did you even look at the documentation? These are matrix operations. Linear Algebra. Come on guy do a little work here. – J. McCabe Nov 17 '16 at 14:30
  • MathNet documentation ? I have been searching for this all the while. Never hit anything as such. Let me look into it again. – opto abhi Nov 17 '16 at 14:31
  • I am so sorry. Thank you so much! I have been struggling with this for ever since yesterday. Even tried various other libs like Accord.NET etc. You are a savior! Do you mind combining both your answers, both of them are relevant and correct. – opto abhi Nov 17 '16 at 14:33