Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing a matrix

Status
Not open for further replies.

member101

Programmer
Nov 10, 2006
3
IE
Hi,

Does anyone know how to return a submatrix from another one? E.g. a 3xn from an nxn.

What I've done is I've defined an 10x10 matrix using pointers. I can access the array elements using the standard notation - A[0][0] - and I can access the first row with A[0]. Is there any way of returning the first three rows?

I suppose there could be a way using pointers - e.g. a pointer pointing to the first three rows - but I still haven't gotten my head round them.

I don't suppose there's any way to access the columns aswell? E.g. A[*][0]? But I'm not too worried about this.

Thanks.
 
You mean like this?
Code:
int a[4][4];
int b[3][3];
int i1, i2;
...
/* Copy a[0-2][0-2] to b. */
for ( i1 = 0; i1 < 3; ++i1  )
{
   for ( i2 = 0; i2 < 3; ++i2  )
   {
      b[i1][i2] = a[i1][i2];
   }
}
 
Not really...wouldn't that cause efficiency problems with large matrices?

Is there a way of getting a pointer to the first 3 rows of a matrix say? Say a matrix occupies memory space from 0 to 1000 (or whatever) -> I want to define a matrix as pointing from 0 to 5, say?
 
A pointer doesn't point to a range, it just points to an address.
If your matrix was 1000 elements in size, you could point to 0 (the beginning), 500 (the middle) or anything else between 0 & 999. How you extract and interpret information from those addresses is up to you.
 
I guess that might help a bit
Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i;
int ** matrix; /*ex 3x3*/
int *copy;
matrix=malloc(3*sizeof(int*));
for(i=0;i<3;i++)
matrix[i]=malloc(3*sizeof(int));
/*copy of 3 row*/
copy=matrix[2];
}

Regards, Martin

(OS and comp.)
using Linux and gcc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top