I am new to Eigen and have limited experience in C++. I have a file which is represented in sparse format (like in LIBSVM) and I want to load the data into a matrix using Eigen. Can someone tell me how to do it ? If you can share your code, it will be really helpful.
So, the file has contents like :
1:13 4:56 9:1
2:45 3:12 5:12 7:2
I want to load this into a matrix using Eigen.
You can use our loadMarket function as an example. The market format looks like this:
%%MatrixMarket matrix coordinate real general
rows cols nnz
1 1 13
1 4 56
1 9 1
2 2 45
2 3 12
2 5 12
2 7 2
with rows, cols, and nnz replaced by the actual number of rows, columns and non-zeros.
Basically, the easiest is to fill a std::vector of triplet (i,j,value), and call SparseMatrix::setFromTriplets to create the sparse matrix.
Another approach would be to write a simple script transforming your format to the market format and call the Eigen's loadMarket function.
Related
I have an assignment where I need to find minimum set cover of some points. I want to be able to store each row of numbers in an individual sets but I do not know the best data structure or approach to do this. I have the number of rows there will be. For example, the .txt file will look like this:
1 2 3 4 5 6
5 6 8 9
1 4 7 10
2 5 7 8 11
3 6 9 12
10 11
Is there a way to dynamically create multiple data structures to store each row of numbers? I was thinking something that works like this if it exists:
list<int> myList[6]; // create 6 lists
myList[0].insert(num); // insert numbers into this list
myList[1].insert(num); //insert numbers into the second list
I do not want to individually create lists because in a .txt file, there can be up to 300 sets of numbers.
edit: my main issue is figuring out how to dynamically create some data structure, preferably if it works with std::set_union since it looks useful to my assignment
If you want to control the number of lists programmatically, you can use a std::vector of datasets. So in your case the declaration would be
std::vector<std::list<int>> lists(6);
Adding new, empty lists to the lists set is done by
lists.push_back({}).
I am trying to do association rule mining using weka tool
The length of record is not same for the data which I have.
Ex of the data:
a,b,c
a,b,c,d,e,f
a
k,g,y,r
In the above example
R1: length is 3
R2: length is 6 and so on
Because of different record lengths. weka is throwing error like
wrong number of values read 3 expected 2.
Please let me know , How can I use Weka for association rule mining when
the length of the record is not same?
Use the sparse arff file format.
ARFF format documentation.
{1 X, 3 Y, 4 "class A"}
see also the example .arff files.
I am writing a code to do some template matching using cv::matchTemplate but I have run into some problems with the 2-dimensional vector of vectors (vov) I created which I have called vvABC. At the moment, my vov has 10 elements which can change based on the values I pass while running the code.
My problem is moving from one column in my vov to the next so I can calculate the size. From my understanding of how vov works, if I have my elements stored in my vov as:
C_A C_B
0 0
1 1
2 2
3
4
5
6
To calculate the size of the first column, I should simply do something like:
vvABC[0].size() to get the size of the first column (which would give 3 in this case) and vvABC[1].size() to get the size of the second column (which would give 7). The problem I am now faced with is both of them give '3' in both cases which is obviously wrong.
Can someone please help me out on how I can get the correct size of the next column?
I stored my detections in my vvABC, now I want to match them one at a time.
It seems like you made a mistake here:
for (uint iCaTemplate = iCa + 1; iCaTemplate < vvABC[iCa].size(); ++iCaTemplate) {
iCa is an index on the 'first level' of vector (of size 2 in your example above), i.e. columns, and you use it to go through the elements of the 'second level' of vector, i.e. rows.
Thanks a lot guys, esp. JGab, after several debug outputs, I finally found that my vector of vectors wasn't being filled up the way I thought it was...thanks once more and my apologies for my belated response.
I have been scouring the internet for quite some time now, trying to find a simple, intuitive, and fast way to approximate a 2nd degree polynomial using 5 data points.
I am using VC++ 2008.
I have come across many libraries, such as cminipack, cmpfit, lmfit, etc... but none of them seem very intuitive and I have had a hard time implementing the code.
Ultimately I have a set of discrete values put in a 1D array, and I am trying to find the 'virtual max point' by curve fitting the data and then finding the max point of that data at a non-integer value (where an integer value would be the highest accuracy just looking at the array).
Anyway, if someone has done something similar to this, and can point me to the package they used, and maybe a simple implementation of the package, that would be great!
I am happy to provide some test data and graphs to show you what kind of stuff I'm working with, but I feel my request is pretty straightforward. Thank you so much.
EDIT: Here is the code I wrote which works!
http://pastebin.com/tUvKmGPn
change size to change how many inputs are used
0 0
1 1
2 4
4 16
7 49
a: 1 b: 0 c: 0
Press any key to continue . . .
Thanks for the help!
Assuming that you want to fit a standard parabola of the form
y = ax^2 + bx + c
to your 5 data points, then all you will need is to solve a 3 x 3 matrix equation. Take a look at this example http://www.personal.psu.edu/jhm/f90/lectures/lsq2.html - it works through the same problem you seem to be describing (only using more data points). If you have a basic grasp of calculus and are able to invert a 3x3 matrix (or something nicer numerically - which I am guessing you do given you refer specifically to SVD in your question title) then this example will clarify what you need to do.
Look at this Wikipedia page on Poynomial Regression
I'm trying to create a simple class for maps in my game. Maps are 2 dimensional vectors made of "tile" structs. I'm trying to figure out the best way to input/output these tiles in rows.
For example, I used to use a .txt based loading format. The .txt file would look like this:
1 0 2 0 0 0 2 4 5 6 3
2 4 5 0 0 0 2 0 0 3 4
0 3 5 2 5 3 0 5 5 3 4
0 2 0 5 0 6 0 5 7 8 4
I would then go line by line to find the ID integer of each tile. 1 would represent grass, 0 would mean water, and so on. When the parser would reach the end of a line, it would skip down to the next line of ints.
Now I'm trying to do this via fwrite and fread with binary files and structs instead of ints. How would I go about doing this? All I've seen is how to store an array of structs in a binary file, not how to store a multidimensional array of structs. Any ideas?
EDIT: Yes, I could just store the 2D vector in the file, but that wouldn't allow me to do seamless map loading, which I need. I have large map files, so having 100k tiles loaded at once would hog CPU.
If you want your map to be human readable, you need to write a parser.
However, if you don't care, you should do something completely different: Use serialization, e.g. from Boost.
A third alternative for your case, which is quite common for games, is to store the map as an image file. You can then edit the map in your favorite image manipulation program, and it is easily interpreted by your program.
Many libs can read image data for you (and present it to you as a 2-D array), and in a game you maybe have something like that already in place.