Hey,

I'm doing a project and on the last little piece.

It involved taking 2 matrices, splitting them into blocks and then checking whether the blocks are simular or not. If the blocks wasn't simular, then swap them /or/ create a new Matrix that holds the values of the second matrix (the correct one).

I have done this, and, been left the right Matrix, but, it's in 2x2 blocks and I need it to be in 4x4 blocks that match the same as the second Matrix. For example:

(The blocks are stored in a vector of vectors)

Matrix2:
0 0 0 1
1 1 0 1
0 1 1 0
0 1 0 1

Final Output:
00
11
01
01
01
01
10
01

I was thinking something like this:

vector<double> formatMatrix(vector<double>& theMatrix)
{
    vector<double> newMatrix(4*4, 0);
    for(int i=0; (i < 4*4); i++)
    {
        newMatrix.push_back(theMatrix[i]);

    }
    return newMatrix;
}

I need it so the final matrix is a 1D vector.

Any ideas? Thanks :)

Please explain your goal more precisely. I understand it as follows:

STEP 1:
00 1 Assignment of numerical identifiers for each 2 x 1 matrix to be combined
11 2 into a 4 x 4 matrix. Note this could also be an 8 x 2 matrix.
01 3
01 4
01 5
01 6
10 7
01 8

STEP 2:
1 5 The 4 x 4 matrix demonstrating how the above 2 x 1 matrixes are related
2 6 using the numerical identifiers as assigned above
3 7
4 8

STEP 3:
Final goal being a 1 dimensional vector representing the 4 x 4 matrix or the 8 individual 2 x 1 matrixes. The following represents how the individual 2 x 1
vectors would be arranged using the numbering system from step one above.
1 5 2 6 3 7 4 8

Not following what you want to do. What does swap them or create a new matrix mean? In formatMatrix the input is a 1D vector, so what happen to the matrix?
Can you post an example with matrix1 and matrix2 input and what the output should be?

Hey thanks for your reply! So yes, that's how I want it.

If the final matrix is this:

00
11

01
01

01
01

10
01

There are four blocks, and these four blocks now need to be placed inside a 1D vector but still maintaining the same order:

0 0 0 1
1 1 0 1
0 1 1 0
0 1 0 1

If that makes sense?

Thanks :)

So if you label the four 2 x 2 matrixes 1-4 is the 4 x 4 matrix developed like this:

1 2
3 4

or like this:

1 3
2 4

And what is the exact format you want to store: a 4 x 4 or a one dimensional vector.

1 2
3 4

Like that, because the matrix would have to be formed as it was orignally displayed.

:)

Something to consider only. Only spewing off the top of my head, but it seems to make sense. Use ideas at your own risk.

Create a vector of 2 x 2 vectors (that is a 3 dimensional vector of vectors). In the example provided this could be thought of as v3[4][2][2].

Use nested loops to read data from 3 dimensional vector into 1 dimensional
vector<int> v1
int i = 0;
for(int a = 0; a < 4; a += 2)//control which 2 x 2 vector to use
for(int b = 0; b < 2; ++b)//control which row of the 2 x 2 vector to use
//get row b from v3[a]
for(int c = 0; c < 2; ++c)//control which column
v1[i++] = v3[a][b][c]; //add to v1
//get row b from v3[a + 1]
for(int d = 0; d < 2; ++d)//control which colum to use from v3[a + 1]
v1[i++] = v3[a + 1][b][d];

Edit: drat. 3 tildes before and after code doesn't preserve indentation and creating 5 leading spaces in edit box doesn't save indentation so you'll have to figure that out yourself.

Edited 4 Years Ago by Lerner

Hey thanks for the reply.

Ahh, I was looking for a way to do it without including offsetX, offsetY (I know it sounds dumb) but they fustrate the hell out of me!

I made a mistake on line 25.

// I have this
cout << subMatrix[q][i*subRow+j] << ' '; // <---- Bad
// Should be subCol
cout << subMatrix[q][i*subCol+j] << ' ';  // <--- Good

It worked because it was a square matrix

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