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 :)