-1

I'm trying to make a simple random matrix multiplication program using the Eigen library in C++, but the numbers I get using the setRandom(); function are floats from -1 to 1 and I want for the numbers to be ints from 0 to 9 instead, how do I do that?

my code:

int mat_size;
cout << "Enter the size of the array: " << endl;
cin >> mat_size;

MatrixXd m = MatrixXd(mat_size,mat_size);
MatrixXd n = MatrixXd(mat_size,mat_size);
m.setRandom();
n.setRandom();

cout << "Matrix m: " << endl << m << endl;
cout << "Matrix n: " << endl << n << endl;
cout << "The product between m and n is: " << endl;
cout << m * n << endl;

and the output for a 3*3 matrix is always:

Matrix m: 
 0.680375   0.59688 -0.329554
-0.211234  0.823295  0.536459
 0.566198 -0.604897 -0.444451
Matrix n: 
   0.10794  -0.270431    0.83239
-0.0452059  0.0268018   0.271423
  0.257742   0.904459   0.434594
The product between m and n is: 
-0.0384828  -0.466066   0.585123
 0.0782496   0.564396   0.280774
-0.0260932  -0.571318   0.113959

I want for them to be something like:

matrix: 
4 8 9
3 2 7
3 8 1
favourites
  • 21
  • 6
  • 2
    Do +1 then /2 then *9 – Neil Kirk Apr 13 '15 at 16:31
  • Don't forget to use a biasless rounding heuristic after those operations. – huu Apr 13 '15 at 16:31
  • Is that funny in america? – favourites Apr 13 '15 at 17:13
  • a) What makes you think he's from America, b) Who are you responding to and why do you think it's not serious? c) What thought have you given to this problem yourself? While the answer provided by Javia1492 is not on target, @NeilKirk's comment is quite useful. Given that you've not put forth any apparent effort here, you're lucky to have his comment. – mah Apr 13 '15 at 17:41
  • I thought they commented sarcastically making some kind of 'joke' of my problem, so I guess I am responding to both of them; america?, that's actually a deeper question, why do people from america become so sarcastic when they know more than others? it's probably their education. I have put the last hours of my day into this matter, if you want the references I've been reading they are as follow: http://comments.gmane.org/gmane.comp.lib.eigen/471 http://eigen.tuxfamily.org/dox/group__TutorialMatrixArithmetic.html http://stackoverflow.com/questions/322938/recommended-way-to-initialize-srand – favourites Apr 13 '15 at 17:48
  • 1
    There's nothing at all sarcastic about @NeilKirk's comment (and I don't think HuuNguyen's comment is sarcastic either though I might be mistaken there). You've way overcomplicated your question, which is highly simplified with "How to I convert a random float in [-1.0, 1.0] to an int in [1, 9], which NeilKirk answered. The references you are reading aren't relevant unless you're reading about converting floats in one range to ints in another -- a pretty trivial operation. – mah Apr 13 '15 at 17:52
  • I haven't yet understood the answer from @NeilKirk , where do I do that? can you help me, oh master, in this, oh so trivial, operation? – favourites Apr 13 '15 at 17:55
  • Actually I was in the middle of providing the one line function that does it based on @NeilKirk's comment, when I received _your_ sarcastic reply, causing me to change my mind. People asking for help, when they are unwilling to put out any thought of their own, ought to consider their words a bit more carefully. Yes, a one line function is trivial, especially when the formula was given to you. Happy coding. – mah Apr 13 '15 at 17:57
  • I'm sick of your gigantic egos, what do they do to you to turn out so unfriendly. I thought Stackoverflow was all about teaching each other in a community of people who want to learn but everyday I find more and more people taking their inflated egos for a walk answering things that are more of a barrier than any help at all, IT'S OKAY if you've learned so much to find problems so "trivial" but what do you do to help really? Anyway I am sorry that you need the approval of random people from the internet, hope you take a chance on being friendly and empathic next time, nobody was born knowing – favourites Apr 13 '15 at 18:09
  • 4
    Seriously what's your problem. Add 1 to the float. Then divide the float by 2. Then multiply the float by 9. Is that not a trivial operation? What's with the rant about Americans? My nationality is irrelevant. You have an attitude problem. – Neil Kirk Apr 13 '15 at 18:26

3 Answers3

1

Here is one possibility that works without explicitly looping through each matrix element:

#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;

int main(){
  int nrow = 8;
  int ncol = 8;
  MatrixXd m = (MatrixXd::Random(nrow,ncol)+MatrixXd::Ones(nrow,ncol))*5;  
  MatrixXi mi = m.cast<int>();    
  cout << mi << endl;  
}

Output:

8 2 6 1 6 5 0 0
3 5 7 4 2 7 9 0
7 4 1 1 6 4 5 4
7 6 6 1 5 8 0 0
9 3 0 9 4 2 1 2
1 5 2 2 9 3 6 9
3 9 1 5 2 8 8 9
7 9 8 8 7 9 3 8
RHertel
  • 23,412
  • 5
  • 38
  • 64
0

You can try converting the floats to ints with something like

float f = m;
float f2 = n;
int i = int(f + 0.5);
int j = int (f2 + 0.5);

It will round it, so it may not be a 100% accurate representation of the Eigen values.

Javia1492
  • 862
  • 11
  • 28
  • 1
    I'm sorry but I'm looking for a more efficient answer using the "Eigen syntax". – favourites Apr 13 '15 at 17:28
  • Well, the Eigen library definition for the Matrices/Vectors are computed as floats and the results of `setRandom()` is generating floats, so you will have to type cast the result or overload the matrix. Also, if your output is always the same, it might need a seed to create a new randomizing order. Otherwise, it will run the same "randomizing" algorithm and generate the same output. Im not too familiar with the Eigen family, but from what i saw as far as the function declarations and structures, you dont have many options other than type casting/overloading. – Javia1492 Apr 13 '15 at 17:49
0

The answer is as follows:

for matrix m:

for(int i = 0; i < mat_size; i++) {
    for(int j = 0; j < mat_size; j++) {
        m(i,j) = static_cast<int>(10.0*abs(m(i,j)));
    }
}

Using 5 as the mat_size this outputs:

Matrix m: 
6 6 0 8 9
2 3 2 2 5
5 5 2 4 7
5 4 0 7 6
8 1 9 2 6

So I just do the same for n and that's it, I hope this can help others in the future.

favourites
  • 21
  • 6