I am trying to extract data from multiple files into one output file.
At the moment I am able to extract the columns I want into a vector in a vector:

vector< vector<double> > out;   //2D vector
ofstream myfile ("example.txt");    //outputfile

//the data contained in data[n][6] is put into the vector called 'out'.
for(int i = 1; i < iNumberOfFiles+1 ; i++) {
	vector<double> row;	
	for (int n = 79; n < data.size(); n++) {
		row.push_back(data[n][6]);
	}  
	out.push_back(row);
}

//print the data in myfile
for(int i=0;i<out.size(); i++) {
	for (int j=0;j<out[i].size(); j++)
		myfile << out[i][j] << " ";
	myfile << endl; 
}
/*
Now here comes my problem, the data is put out as
a1	a2	a3	….	aN
b1	b2	b3	….	bN
.	.	.	….	...
x1	x2	x3	…	xN

but I need the data in the following form:
a1	b1	.	x1
a2	b2	.	x2
.	.	.	.
aN	bN	.	xN
*/

//I though the data could be transposed like  a 'normal' 2d array
vector< vector<double> > outtrans;   //the 'transposed' vector
for(int i=0;i<out.size(); i++) {   
	for (int j=0;j<out[i].size(); j++){
	        outtrans[j][i] = out [i][j];
	}
}
/* this is not working well however, when running this code, it (windows) says
file.exe is not functioning properly */

Does anyone have tips of how to transpose my vector properly (vector< vector<double> > out)?

If you wanna do a quick n dirty transpose on output just switch your i and j values when printing out. From out[i][j] to out[j][i] will output the transposed matrix assuming that it is a square (#rows = #cols).

If you don't know if #rows = #cols then you do what you are doing now but you must allocate memory for your vector before trying to access it. You can set the size of outtrans by writing

vector< vector<double> > outtrans(out[0].size(), vector<double>(out.size()));

This creates a new 2D vector with the reversed dimensions of "out".

Many thanks for your quick reply!

Indeed, in most cases the number of rows and columns are different. So me not allocating the memory properly at the 'transpose' caused the program to crash at the point where I was trying to print it.

Now it is working properly :)

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.