Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have read a text file now I want to insert its values into the matrix. The values are tab separated in the form of rows and columns my data is :

5.000000 2.400000 4.000000 1.000000 0.800000
113.000000 94.320000 54.000000 59.000000 118.400000
384.000000 474.460000 330.000000 54.000000 158.050000
178.000000 185.710000 46.000000 132.000000 279.200000
174.000000 218.780000 61.000000 113.000000 255.000000
0.000000 0.000000 0.000000 0.000000 0.000000

I am inserting the values as:

C++
#include <conio.h>
#include <io.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <string>
#include <vector>
#include <stdexcept>
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
using namespace cv;
using namespace cv::ml;

Mat CreateVector()
{
int Samples = 50, fea= 7;
Mat Vector(Samples, fea, CV_32FC1);
std::ifstream infile("table.txt");	
try
{
std::string line;
int rowNum = 0;
while (std::getline(infile, line))
{
	std::istringstream iss(line);
	int a, b, c, d, e, f, g;
	if (!(iss >> a >> b >> c >> d >> e >> f >> g)) { break; } // error
	// process pair (a,b,c,d,e,f,g)
	String Values[50][7] = line.split(" ");
	for (int colNum = 0; colNum <= 10; colNum++)
	{
				
	Vector.put(rowNum, colNum, (int)FeatureValues[colNum]);
	Vector.at(rowNum, colNum, (int)FeatureValues[colNum]);
	Vector.insert(rowNum, colNum, (int)FeatureValues[colNum]);
	Mat::inserter (rowNum, colNum, (int)FeatureValues[colNum]);
	}
	rowNum++; //Enter the second vector Values
}
infile.close();	//Close 
}
catch (Exception ex)
{ }
}


Kindly tell me how to split the values on tabs to get discrete values row by row. Since I am getting the codes in java which looks like this:
String FeatureValues[50][7] = line.split(" ");

Secondly guide me how to insert values of my csv file in Matrix named vector

What I have tried:

I have tried inserting the values into my vector using four ways yet after extensive google search i.e.

Vector.put(rowNum, colNum, (int)FeatureValues[colNum]);
Vector.at(rowNum, colNum, (int)FeatureValues[colNum]);
Vector.insert(rowNum, colNum, (int)FeatureValues[colNum]);
Mat::inserter (rowNum, colNum, (int)FeatureValues[colNum]);

but none is working while I guess put is function of java since working fine with java but I want in cpp
Posted
Updated 23-Apr-16 5:04am
v2

1 solution

I modified your code a bit to migrate it further from Java to C++. Here is what I've got:
C#
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>

using namespace std;

void CreateVector(vector<vector<double> > &res)
{
	ifstream infile("table.txt");
	string line;
	while (getline(infile,line)) // Read a line
	{
		res.push_back(vector<double>()); // Add a new row to the matrix
		vector<double>& row = res.back();
		istringstream iss(line);
		double value;
		while (iss >> value) // Read columns
			row.push_back(value); // Add a column to the current row
	}
	infile.close();
}

To test it, you may need a function to print the output and the main function:
C++
// This function is just for printing the result
void PrintVector(const vector<vector<double> > &res) {
	for (auto row : res) {
		for (auto value : row)
			cout << value << "\t";
		cout << endl;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	vector<vector<double> > mat;
	CreateVector(mat);
	PrintVector(mat);
	char c;
	cin >> c;
}

The matrix is represented as vector of vectors data structure (sparse array). PrintVector function should also give you an idea on how to iterate items of this array.
 
Share this answer
 
Comments
KittoKy 23-Apr-16 11:05am    
my main problem is that I want a matrix using OpenCv since this matrix is the input of my algorithm SVM designed using OpenCv

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900