I have an mxn matrix, that is stored in memory in an array:

matA = new float[m * n];
// ... Assign values
matA[0] = 1;
matB[1] = 2;
// ...

After I finish with this array, I then need to create very similar array to store a new mxn matrix--the same matrix as before, but with each column shifted to the right. Let's call it matB.

Am I better of to create a new matrix?

matB = new float[m * n];
// ... Assign values
matB[0] = 23;
matB[1] = 1;
matB[2] = 2;
// ...

Or is there a more efficient way to shift the values in the matrix?

Recommended Answers

All 4 Replies

Couple things:
1. I can't figure out what you want to do exactly
2. arrays declared on the stack (i.e. like so: int arr[50]) HAS to have its length be a constant. So either a literal or a const variable. Instead use malloc(sizeof(int))*(m*n) or something similar. return it from a function or something: int * new_arr(int m,int n) { return malloc(sizeof(int))*(m*n); }

Unfortunately I have to double post because I didn't read carefully enough. I apologize
Something like this maybe?:

int i,k;
for(i=0;i<m*n;i+=m)
{
    for(k=i+m;k>i;k--)
    {
        arr[k]=arr[k-1];
    }
    arr[k]=0;
}
Member Avatar for iamthwee

Do you mean something like this

Sorry for not responding sooner-I didn't realize my e-mail alerts were turned off.

Dancks, yes, that method is probably better than allocating more new memory.

iamthwee, thanks, that is the kind of link I was looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.