Matrix

Apr 11, 2013 at 9:33am
Hi everybody

I have some questions about matrix.

1- how can I load a matrix from another matrix? I mean, is there any function that I ca write A = B; for example. I do not want to use "for loop".

2- How can I select a row of a matrix in C++?

3- how can I add a row or column to a matrix? For example, I have matrix A like below
A =
1 2 3
4 5 6
7 8 9

and I want to add another column to this matrix like
A =
1 2 3 8
4 5 6 5
7 8 9 7
Last edited on Apr 11, 2013 at 9:34am
Apr 11, 2013 at 9:40am
You need to write a class Matrix that to be able to use such operations.
Apr 15, 2013 at 2:36pm
Thank you Vlad for your respons

Actually I wrote a code with pointer but it is not working properly.
First, I get the matrix and row supposed to be added to the matrix from user and then add the row to the matrix with pointer.

The problem is that when I want to show the matrix the value of last row is not correct . Here is my code. I do not know what is the problem. I guess that I may be do not use the pointer properly but I dont know where and why?

#include <iostream>

using namespace std;

class MatrixClass
{
public:
int* addRC(int *MPtr, int * RC, int s, int R, int C, char T)
{
//R: The number of Row C: The number of Column
//T(R/C): Adding Row or Column RC: Row or Column should be added
int NE(R*C); // NE: Number of Elements in the old matrix

for (int i=0; i<(C); ++i)
{
NE = i + NE;
*(MPtr + NE) = RC[i];
cout << *(MPtr + NE) << "\t";
cout << (MPtr + NE) << "\t";
}
cout << endl;
return(MPtr);
}
};

int main()
{
int A[2][3], E[3], *APtr, *SS;

cout << "Enter the Matrix" << endl;

for (int i=0; i<2; ++i)
{
for (int j=0; j<3; ++j)
{
cin >> A[i][j];
}
}
cout << "Enter the additinal row" << endl;
for (int i=0; i<3; ++i)
{
cin >> E[i];
}
cout << "APTR= " << APtr << endl;
MatrixClass AAA;

SS = AAA.addRC(APtr, E, 3, 2, 3, 'R');

for (int i=0;i<9;++i)
{
cout << SS << "\t";
cout << *SS;
SS = SS + 1;
cout << endl;
}
return 0;

}
Apr 15, 2013 at 3:47pm
Firstly, when you post code, please enclose it in code tags, so that we can read it more easily.

Your MatrixClass design seems very strange. It doesn't seem to actually own any data, and it seems to consist of just one method. Effectively, you've written a global function, and wrapped it in a class for no good reason. Any particular reason why?

It looks to me like you're writing data beyond the end of the memory you've allocated on the stack for your array. You declare an array of dimensions 2 x 3, i.e. 6 elements. But then in your addRC method, you start writing to the address at (MPtr + NE). On the first pass through the loop, this will be MPtr + 6, which is beyond the end of the array. This is not correct, and will cause values to be written into memory addresses which may be being used for other things.
Apr 16, 2013 at 5:06am
Dear MikeyBoy

Thank you for your reply. Actually, I am beginner in C++ and here as well so was not familiar with the regulation here and that is why I put my code like that.

In fact I have a very big matlab code. and I want to convert it to C++ to speed up the code. In that code I add and remove a either row or column of matrices. So I just try to write an example code to do this function.

1- I understand that we cannot return more that one value (like a matrix). So I use pointer.
2- I understand that a matrix has been stored in consecutive memory addresses like a vector. That is why I tried to add the new row at the end of matrix. (This is the purpose of using (MPtr + NE). Of course I understand your point but I do not know how to figure it out).
Apr 16, 2013 at 8:03am
I'm not familiar with Matlab, but from memory, it's a scripting language designed to make it easy to perform complex maths operations. I presume that there's built-in memory management, such that if you simply change the size of a matrix, it automatically allocates more memory for you.

C++ is different. It's sophisticated and powerful, and one of the aspects of it that makes it sophisticated and powerful is that the memory management is entirely in the hands of the developer. However, this means that if you start treating an object as if it was bigger than the memory you originally allocated for it, it won't automatically increase the size of that memory. You have to handle that yourself.

I understood exactly what you are doing with that pointer arithmetic, and it would be correct if you already had that extra memory allocated to store the extra row. If you're changing the size of your matrix by adding another row to it, then you'll need to dynamically allocate new memory for it.

Of course, that pointer arithmetic won't work at all when it comes to adding a new column - but that's a whole separate issue.

Whatever tutorial source you're using to learn C++ will, no doubt, have a section on dynamic memory allocation. There's also a brief tutorial on this very site:

http://www.cplusplus.com/doc/tutorial/dynamic/

Edit: Regarding your first point, it's perfectly possible to return a single object, such as a MatrixClass object, from a function. However it's not necessarily the best thing to do. If your function is going to reallocate the memory for the matrix, then returning a pointer is probably the correct thing to do.

Last edited on Apr 16, 2013 at 10:32am
Topic archived. No new replies allowed.