I'm trying to design a program that creates a matrix using vectors of vectors of integers, and then multiplyies it with another matrix. I know how to multiply matrices on paper, but when I try to implement it in my program, I'm not getting it to work. I know that both matrices are entered correctly and are passed correctly as I have the the output of those functions so that I can debug. The program works incorrectly when I try to multiply them. The answer and the number of elements are not right. I know I'm missing something but can't figure out what.
Matrix Matrix::operator*(Matrix m){
vector<int> mRow = m.getRow(0);
vector<int> mCol = m.getCol(0);
vector<int> newElem;
int product = 0;
//adds the contents of the 2nd matrix to the 2d vector
vector< vector<int> > m2(mRow.size(), vector<int>(mCol.size()));
for (int i = 0; i < mRow.size(); i++){
mRow.clear();
mRow = m.getRow(i);
for (int j = 0; j < mCol.size(); j++){
m2[j][i] = mRow[j];
}
}
//Multiplies the matrices using the 2d matrix**THIS IS WHERE IT GOES WRONG**
for (int i = 0; i < row; i++){
for (int j = 0; j < column; j++){
product += matrix[i][j]*m2[j][i];
}
newElem.insert(newElem.begin()+i,product);
product = 0;
}
//displays the products so that i can see if its working
for (int i = 0; i < newElem.size(); i++){
cout << " "<<newElem[i]<<endl;
}
//adds the new product vector to a new Matrix object and returns it
Matrix newM(row, mCol.size());
vector<int> temp;
for (int i = 0; i < row; i++){
for (int j = 0; j < mCol.size(); j++){
temp.insert(temp.begin()+j, newElem[0]);
newElem.erase(newElem.begin());
}
newM.setRow(temp,i);
temp.clear();
}
return newM;
}
Although I don't know whether this helps, I'm using this site as a reference for multiplying 2 matrices together.
Your matrix representation has nothing to do with your mistake. You need to have more nested iterations. Think of a result matrix and iterate through that to calculate it's every element. In a pseudocode:
for i in result column
for j in result row
res[i, j] = multiply(m1, m2, i, j)
where multiply function is the nested loop, something like this:
multiply(m1, m2, i, j)
{
val = 0;
for k in row
val += m1[i, k] * m2[k, j]
return val
}
Here is an implementation of the outer loops. Mind you, there are no error checking in the code.
vector<vector<int> > ml;
vector<vector<int> > mr;
// fill in ml and mr
...
// result matrix
vector<vector<int> > res;
// allocate the result matrix
res.resize(ml.size());
for( it = res.begin(); it != res.end(); ++it)
it->resize(ml[0].size());
// loop through the result matrix and fill it in
for( int i = 0; i < res.size(); ++i)
for( int j = 0; j < res[0].size(); ++j)
res[i][j] = multiply(ml, mr, i, j);
Leaving a proper implementation of multiply() function to you.
Related
I am trying to rotate a 2d vector clockwise but I am unable to get it to work for vectors that are not square.
Here is what I tried
bool Pgm::Clockwise(){
int i, j, k;
vector <vector <int> > tempPixel;
for (i = 0; i < Pixels.size(); i++) {
tempPixel.push_back(Pixels.at(i));
}
for (j = 0; j < tempPixel.size(); j++) {
tempPixel.push_back(vector<int>());
for (k = 0; k < tempPixel.at(0).size(); k++) {
tempPixel.at(j).push_back(Pixels.at(j));
}
}
return true;
}
#include <vector>
std::vector<std::vector<int>> rotateClockwise(std::vector<std::vector<int>> v) {
int n = v.size();
int m = v[0].size();
// create new vector with swapped dimensions
std::vector<std::vector<int>> rotated(m, std::vector<int>(n));
// populate the new vector
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
rotated[j][n-i-1] = v[i][j];
}
}
return rotated;
}
This function takes a 2D vector as input and returns a new 2D vector that is rotated 90 degrees clockwise. It first creates a new vector rotated with dimensions swapped, then populates it by copying the elements from the input vector in the appropriate order.
i hope this will help you to get an idea..
I would like to ask how to use the dft function in opencv to do Fourier transform with 2d vector.
I have error in my code ...
thank you all
//2d vector in here v1 and v2
//vector size is 1000*1000
//i have to do about 600 images in this process
for (int k = 0; k < 600; k++){
vector <vector <complex < double >>> v1(InputWidth, vector<complex < double >>(InputHeight));
vector <vector <complex < double >>> v2(InputWidth, vector<complex < double >>(InputHeight));
for (int i = 0; i < InputWidth; i++) {
int tempIndex = i*InputHeight;
for (int j = 0; j < InputHeight; j++)
{
int Correction_value = ProjectionImage[k]->data.s[tempIndex + j] * meshGrid[tempIndex + j];
ProjectionImage[k]->data.s[tempIndex + j] = Correction_value;
//v1 is an image
v1[i][j] = Correction_value;
}
}
//error happen in here
dft(v1, v2, DFT_COMPLEX_OUTPUT);
//do frequency filter
for (int i = 0; i < InputWidth; i++) {
for (int j = 0; j < InputHeight; j++)
{
v2[i][j] *= filter_2D[i][j];
}
}
//inverse fourier transform
idft(v2, v1, DFT_COMPLEX_OUTPUT);
}
error message
What is happening here is that you create a std::vector of pointers to other vectors. Those other vectors contain the pixel data. But those data are therefore not necessarily contiguous. Each row (or column) of your image is stored in its own memory segment. OpenCV expects all pixels to be in the same memory segment.
In short, simply copy your pixel data to a form supported by OpenCV, such as a cv::Mat.
So far I have this code for an LU decomposition. It takes in an input array and it returns the lower and upper triangular matrix.
void LUFactorization ( int d, const double*S, double*L, double*U )
{
for(int k = 0; k < d; ++k){
if (
for(int j = k; j < d; ++j){
double sum = 0.;
for(int p = 0; p < k; ++p) {
sum+=L[k*d+p]*L[p*d+j];
cout << L[k*d+p] << endl;
}
sum = S[k*d+j] - sum;
L[k*d+j]=sum;
U[k*d+j]=sum;
}
for(int i = k + 1; i < d; ++i){
double sum=0.;
for(int p = 0; p < k; ++p) sum+=L[i*d+p]*L[p*d+k];
L[i*d+k]=(S[i*d+k]-sum)/L[k*d+k];
}
}
for(int k = 0; k < d; ++k){
for(int j = k; j < d; ++j){
if (k < j) L[k*d+j]=0;
else if (k == j) L[k*d+j]=1;
}
}
}
Is there some way I can adapt this to perform row exchanges? If not, is there some other algorithm I could be directed towards?
Thanks
The usual approach for LU decompositions with pivoting is to store a permutation array P which is initialized as the identity permutation (P={0,1,2,...,d - 1}) and then swapping entries in P instead of swapping rows in S
If you have this permutation array, every access to S must use P[i] instead of i for the row number.
Note that P itself is part of the output, since it represents the permutation matrix such that
P*A = L*U, so if you want to use it to solve systems of linear equations, you'll have to apply P on the right-hand side before applying backward and forward substitution
i want to potentiate a Matrix but i dont workings how it should work.
m ist the Matrix i want to potentiate
long double pro[100][100]; // product after each step
long double res[100][100]; // the Matrix with the exponent n
for (int n = 1; n < nVal; n++) // exponent
{
for (int i = 0; i < mVal; i++) // row
{
for (int j = 0; j < mVal; j++) // col
{
res[i][j] = 0;
for (int k = 0; k < mVal; k++) // inner
{
res[i][j] += pro[i][k] * m[k][j]; // multiply the product with the default matrix
}
}
}
}
// array Output - working
for (int i = 0; i<mVal; i++)
{
for (int j = 0; j<mVal; j++)
cout << res[i][j] << "\t";
cout << endl;
}
in the output i see some crazy numbers and i dont know why :(
Can anyone help me?
You should
initialise the pro matrix to the identity at the beginning of loop on n
copy the res matrix into the pro matrix the end of each loop on n.
In pseudo code
pro = Identity matrix
for (int n = 1; n < nVal; n++) {
res = pro * m // using two loops
pro = res
}
result is in pro.
Note that there are much faster way to compute powers: http://en.wikipedia.org/wiki/Exponentiation_by_squaring
As Willll said you shouldn't forget to initialize.
Another suggestion would be to erase the exponent loop and just use the pow() function from math library. It´ll make it more simple and easier to visualize.
So what I am trying to do is multiply one 2d vector by another 2d vector.
I come from Java,Python and C# so I am pretty much learning C++ as I go along.
I have the code down to generate the vector and display the vector but I can't seem to finish the multiplication part.
v1 is another matrix that is already generated.
vector<vector<int> > v2 = getVector();
int n1 = v1[0].size();
int n2 = v2.size();
vector<int> a1(n2, 0);
vector<vector<int> > ans(n1, a1);
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
for (int k = 0; k < 10; k++) {
// same as z[i][j] = z[i][j] + x[i][k] * y[k][j];
ans[i][j] += v1[i][k] * v2[k][j];
}
}
}
displayVector(ans);
My guess for where I am going wrong is in the inner-most loop. I can't figure out what to actually put in place of that 10 I have there now.
When you multiply matrices, the number of columns of the matrix on the left side must equal the number of rows of the matrix on the right side. You need to check that that is true, and use that common number for your size of the k variable:
int nCommon = v1.size();
assert(v2[0].size() == nCommon);
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
for (int k = 0; k < nCommon ; k++) {
ans[i][j] += v1[i][k] * v2[k][j];
}
}
}
For you inner loop, you should do something like this
ans[i][j] = 0;
for (int k = 0; k < n2; k++) {
ans[i][j] += v1[i][k] * v2[k][j];
}
I don't know where the 10 comes from.