c++sortingmatrixoctave

Combine two matrices together and organize with one value then the other


I am writhing a code in OCTAVE(C++) where I have 2 matrices that I want merge together. I need to have the values from the first matrix to be intersected with the values from the second matrix. I'm struggling to explain this in words, so hopefully my real world data below will help.

matrix_1 = [67.06582, 14.52026,  0.00000, -5.26088, -19.44268, -21.60000]
matrix_2 = [77.79444, -0.00000,  -0.61309, -10.46707, -21.60000, -20.30657]

output = [67.06582, 77.79444, 14.52026, -0.00000, 0.00000, -0.61309.......]

What is the best way to accomplish this?


Solution

  • You can use Octave to simply concatenate the two arrays along the first dimension (using cat) and then reshape the result into a row vector using reshape. Since Octave uses column-major ordering, it will interleave the two vectors in the result

    reshape(cat(1, matrix_1, matrix_2), 1, [])
    

    If you instead want to do this in C++, you'll need to provide some more code which gives us more information on the data structures that hold this data.