Sailanarmo
Sailanarmo

Reputation: 1181

How to create a Matrix of random Doubles using Vectors?

I'm having trouble creating a Matrix using Vectors. The rows and columns will all be random doubles and I am trying to fill it in.

I have tried to initialize the size of the vector before hand, I believe it initializes correctly, however, when I try to push onto the rows and columns I get very odd output. Here is my Header File:

#ifndef MATRIX_NORM_HPP
#define MATRIX_NORM_HPP

#include <vector>

class MatrixNorm
{

public:
    void initProgram();
    void printResults();

    double randNumber();

private:

    std::vector<std::vector<double>> M;
    double mNorm1 = 0.0;
    double mNormInf = 0.0;

};

#endif

Here is my CPP File:

#include "matrixNorm.hpp"
#include <iostream>
#include <random>

void initProgram()
{
    double ranNum = 0.0;
    int size = 0;   
    std::cout << "Please enter a size of an n by n Matrix: ";
    std::cin >> size;

    std::vector<std::vector<double>> temp(size, std::vector<double>(size));


    for(int i = 0; i < size; ++i)
    {
        for(int j = 0; j < size; ++j)
        {
            ranNum = randNumber();
            temp[i].push_back(ranNum);
            temp[j].push_back(ranNum);
        }
    }

    M = temp;
    printResults();     

}


void MatrixNorm::printResults()
{
    for(auto &&e: M)
    {
        for(auto && f: e)
        {
            std::cout << f << " ";
        }
        std::cout << std::endl;
    }
}

double MatrixNorm::randNumber()
{
    double ranDouble = 0.0;
    std::random_device rd;
    std::default_random_engine generator(rd());
    std::uniform_real_distribution<double> unif(-1000.0,1000.0);

    ranDouble = unif(generator);

    return ranDouble;
}

The output I receive when I run the program from main.cpp is:

Please enter a size of an n by n Matrix: 3
0 0 0 792.208 792.208 -361.248 -776.871 742.521 116.732 
0 0 0 -361.248 742.521 411.965 411.965 909.313 -50.0048 
0 0 0 -776.871 909.313 116.732 -50.0048 79.6189 79.6189 

As you can see, it seems to get the column size correctly, but it does not get the row size correctly, and if you look very closely. Some of the numbers are duplicates, I wish I knew how to format it more clearly but if you start at the top left you see 792.208 792.208 then go down a row and you see 411.965 411.965 and last it finishes off at 79.6189 79.6189 in the lower right.

What am I doing wrong? How do I do this correctly? Any help would be appreciated.

Upvotes: 0

Views: 102

Answers (1)

Amadeus
Amadeus

Reputation: 10655

Seems to me that the correct way to initialize your matrix is:

(...)
std::vector<std::vector<double>> temp;

for(int i = 0; i < size; ++i)
{
    std::vector<double> k;
    for(int j = 0; j < size; ++j)
    {
        ranNum = randNumber();
        k.emplace_back(ranNum);
    }
    temp.emplace_back(k);
}
(...)

Explanation:

with this constructor:

std::vector<std::vector<double>> temp(size, std::vector<double>(size));

you are creating size copies of default vector constructed of size elements (std::vector<double>(size)). In other words, you have a size x size matrix.

So, instead of pushing new values in your code, you should be changing it. In the code I proposed, it is just simpler to populated this matrix when you are creating it.

Upvotes: 2

Related Questions