Hello,

I'm trying to split a matrix (512x512) into blocks of (16x16)..

Now I have managed to complete this for a small matrix (4x4) but when I try to implement it using the 512x512 it fails and doesn't produce enough blocks for the entire matrix. Here is the code on the small matrix:

#include <iostream>
#include <fstream>
#include <sstream>
#include <iterator>
#include <vector>

using namespace std;

typedef vector<double> Matrix;

void subMatrix(const Matrix& mat, int subRow, int subCol)
{
    Matrix subMatrix(subRow * subCol, 0);
    int offsetX = 0;
    int offsetY = 0;
    for(int q=0; (q < (4)); q++)
    {
        for(int i=0; (i < subRow); i++)
        {
            for(int j=0; (j < subCol); j++)
            {
                int row = offsetX + j;
                int col = offsetY + i;
                int sum = subRow+subCol;
                //cout << row+col*sum;

                subMatrix[i*subRow+j] = mat[row+col*sum];

                cout << subMatrix[i*subRow+j] << ' ';
            }
            cout << endl;
        }
        cout << endl << endl;
        offsetX = offsetX*subRow+subRow;
        if(offsetX > 2)
        {
            offsetX = 0;
            offsetY = offsetY*subCol+subCol;
        }

        cout << endl;
    }
    cout << endl;

    for(int i=0; (i < subRow); i++)
    {
        for(int j=0; (j < subCol); j++)
        {
            //cout << subMatrix[i*subRow+j] << ' ';
        }
    }

}

int main(int argc, char *argv[]) {

    string theFile = "matrix1.txt";
    vector<double> values(4*4, 0);
    vector<double> theData;
    ifstream file(theFile.c_str());
    if(!file.is_open())
    {
        cerr << "File cannot be open";
        return 0;
    }
    while(!file.eof())
    {
        copy(istream_iterator<double>(file), istream_iterator<double>(), back_inserter(theData));
    }

    values = theData;
    subMatrix(values, 2, 2);
}

And the code on the 512x512..

#include <iostream>
#include <fstream>
#include <sstream>
#include <iterator>
#include <vector>

using namespace std;

typedef vector<double> Matrix;

void subMatrix(const Matrix& mat, int subRow, int subCol)
{
    Matrix subMatrix(subRow * subCol, 0);
    int offsetX = 0;
    int offsetY = 0;
    for(int q=0; (q < (8)); q++)
    {
        for(int i=0; (i < subRow); i++)
        {
            for(int j=0; (j < subCol); j++)
            {
                int row = offsetX + j;
                int col = offsetY + i;
                int sum = subRow+subCol;
                //cout << row+col*sum;

                subMatrix[i*subRow+j] = mat[row+col*sum];

                cout << subMatrix[i*subRow+j] << ' ';
            }
            cout << endl;
        }
        cout << endl << endl;
        offsetX = offsetX*subRow+subRow;
        if(offsetX > 16)
        {
            offsetX = 0;
            offsetY = offsetY*subCol+subCol;
        }

        cout << endl;
    }
    cout << endl;

    for(int i=0; (i < subRow); i++)
    {
        for(int j=0; (j < subCol); j++)
        {
            //cout << subMatrix[i*subRow+j] << ' ';
        }
    }

}

int main(int argc, char *argv[]) {

    string theFile = "matrix512.txt";
    vector<double> values(512*512, 0);
    vector<double> theData;
    ifstream file(theFile.c_str());
    if(!file.is_open())
    {
        cerr << "File cannot be open";
        return 0;
    }
    while(!file.eof())
    {
        copy(istream_iterator<double>(file), istream_iterator<double>(), back_inserter(theData));
    }

    values = theData;
    subMatrix(values, 16, 16);
}

Anyone see where I'm going wrong? Thanks :)

Recommended Answers

All 2 Replies

Your number of sub-matrices isn't right. I think below is right but test it and test it some more.

#include <iostream>
#include <fstream>
#include <sstream>
#include <iterator>
#include <vector>

using namespace std;

typedef vector<double> Matrix;

// Had to pass input matrix size and output matrix size
// Not using matRow but passed it any way
void subMatrix(const Matrix& mat, int matRow,int matCol,int subRow, int subCol) {
    // Compute the number of sub matrices
    int numSubMatrices(mat.size()/(subRow * subCol));
    cout << "Allocating " << numSubMatrices << endl;
    Matrix *subMatrix(new Matrix[numSubMatrices]);
    int offsetRow(0), offsetCol(0);
    // Loop thru all subs
    for(int q(0); q < numSubMatrices; q++) {
        subMatrix[q].resize(subRow * subCol,0);
        for(int i(0); i < subRow; i++) {
            for(int j(0); j < subCol; j++) {
                subMatrix[q][i*subCol+j] = mat[offsetRow+i*matCol+j+offsetCol];
                cout << subMatrix[q][i*subRow+j] << ' ';
            }
            cout << endl;
        }
        cout << endl;
        offsetCol += subCol;
        if(offsetCol >= matCol) {
            offsetRow +=(matCol*subRow);
            offsetCol = 0;
        }
        cout << endl;
    }
    cout << endl;
    delete [] subMatrix;
}

int main(int argc, char *argv[]) {
    string theFile("matrix512.txt");
    vector<double> theData;
    ifstream file(theFile.c_str());
    if(!file.is_open()) {
        cerr << "File cannot be open";
        return 0;
    }
    try {
       while(!file.eof()) {
           copy(istream_iterator<double>(file), istream_iterator<double>(), back_inserter(theData));
       }
       // Need the NxM of input matrix and sub-matrices
       subMatrix(theData, 512, 512, 16, 16);
    } catch (bad_alloc){
       cout << "Out of memory!" << endl;
    }
}

Output of first sub-matrix

0    1    2    3    4    5    6    7    8    9    10   11   12   13   14   15
512  513  514  515  516  517  518  519  520  521  522  523  524  525  526  527
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087
3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599
4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111
4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623
5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135
5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647
6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159
6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671
7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183
7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.