LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Matrix inversion fails if defined size is higher than actual.

Dear Forum,
 
I'am new here and not a program developer nor a native English speaker, but very soon I have to figure out this problem. If I define a matrix like double M[20][20];, and fill all the elements, and set N into 20, any matrix operation is succesful. But if I fill only let say N=4 rows and lines and set 4 in the functions, any of the matrix functions in the advanced analysis library fails. What could be the reason?
 
Or, how to define a higher size and later have a smaller but any size vector or matrix?
 
Thank You for Your help in advance!
 
regards, Elwood
0 Kudos
Message 1 of 4
(3,156 Views)
The reason is how the elements are packed in memory versus how the library expects them. In short, it expects them to be contiguous in memory. If you declare an 20x20 array, the memory layout will be something like this:

X X X X X X X X X X X X X X X X X X X X
X X X X X X X X X X X X X X X X X X X X
X X X X X X X X X X X X X X X X X X X X
X X X X X X X X X X X X X X X X X X X X
X X X X X X X X X X X X X X X X X X X X
... 14 more rows
X X X X X X X X X X X X X X X X X X X X


Now, if you write a 4x4 array into this using the array indexing operators, you end up with something like this (where an 'X' represents a real data value, and an 'o' represents an unused memory location):

X X X X o o o o o o o o o o o o o o o o
X X X X o o o o o o o o o o o o o o o o
X X X X o o o o o o o o o o o o o o o o
X X X X o o o o o o o o o o o o o o o o
o o o o o o o o o o o o o o o o o o o o
... 14 more rows
o o o o o o o o o o o o o o o o o o o o


So, you end up with alot (MAX_N - N) elements of what amounts to per row packing. The red elements below are where the analysis library would expect the elements of a 4x4 array to be stored (in the first 16 spots).

X X X X o o o o o o o o o o o o o o o o
X X X X o o o o o o o o o o o o o o o o
X X X X o o o o o o o o o o o o o o o o
X X X X o o o o o o o o o o o o o o o o
o o o o o o o o o o o o o o o o o o o o
... 14 more rows
o o o o o o o o o o o o o o o o o o o o


So, you either have to use a different array size that correctly matches what the library expects, or index the memory differently to achieve the expected layout.

Hope this helps,

-alex
Message 2 of 4
(3,147 Views)

Yes, I think that will help. Thank You very much Alex!

Elwood

0 Kudos
Message 3 of 4
(3,127 Views)

Elwood,

Based on your stated lack of programming experience, I'll just throw in a couple of tips. To create an array of the correct size, use the calloc() function, which allocates and initalizes to zero an array of the required size. When you are done, be sure to use free() to free the memory.  A few key lines might look like this:

double *hh;

 // Create dynamic array
 hh = calloc (numrows*numcols, sizeof(double));
 if (hh == NULL) {
  MessagePopup("Error", "calloc error");
  return;
 }

//Copy data from a larger static array H 

offset=0;

for (ii=0; ii< numrows; ii++) {
  for (jj=0; jj < numcols; jj++) {
   *(hh+ offset) = H[ii][jj];
   offset++;
  }
 }
 
// Perform library operation here 

// Free the memory when done

free (hh);

Message 4 of 4
(3,107 Views)