I've been struggling over this for hours, and just can't seem to figure out a formula for it.

I'm trying to append below, (attach underneath) one matrix to the other.

So for example, for Matrix A:

1 2 3
4 5 6

and Matrix B:

7 8 9

A.Append(B) becomes:

1 2 3
4 5 6
7 8 9

So I've started off by resizing A to the size of both matrices, to get

1 4 2 5 3 6 _ _ _

and then the finished vector should like

1 4 7 2 5 8 3 6 9

so I've worked out to get this I need:

data[8] = b.data[2]
data[5] = b.data[1]
data[2] = b.data[0]

which isn't so bad but im struggling with:

data[7] = data[5]
data[6] = data[4]
data[4] = data[3]
data[3] = data[2]
data[1] = data[1]
data[0] = data[0]

since I need to decrease i by 1 but then 'skip' certain i values, can anyone please give me a hint as to how this would be done?

Thanks in advance!

Matrices in C/C++ are laid out rowwise (versus columnwise like you are doing,used in FORTRAN and probably others, I think) first one row, then the next, etc. So expanding your first matrix would look like 1 2 3 4 5 6. There's no need for rearranging.
Here's a nice diagram.

Edited 6 Years Ago by jonsca: n/a

Are you trying to use a one dimensional array of integers to represent a two dimensional array? If yes, then matrix A would be represented as 1 2 3 4 5 6, and adding matrix B would make it 1 2 3 4 5 6 7 8 9.

To find a value in row n column m in a matrix with X columns and Y rows, the general formula ((n-1) * X) + m [edit]And what ^^^ said too[/edit]

Edited 6 Years Ago by Ancient Dragon: n/a

Thanks for the replies both of you, but unfortunately I *have* to use column major order.

Even I were to use row major order, I would encounter problems when trying to append a matrix to the right of another matrix.

This article has been dead for over six months. Start a new discussion instead.