LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Labwindows inverse matrix problem

Solved!
Go to solution

I have a LabWindows program that reads in values to a 2D array, then calculates the inverse using InvMatrix.  My problem is that I do not know before the program starts how many values will be read in.  So, I declare my matrix to be large enough to handle the maximum number of values there might be.  Then, when I call InvMatrix, I get a singular matrix error because the array has some rows and columns of all zeros.  I know that the InvMatrix function has an input for matrix size, but setting that value to the size of the non-zero part of the matrix doesn't seem to solve the problem.  Any ideas how I can get around this?  Thanks!

0 Kudos
Message 1 of 3
(3,613 Views)
Solution
Accepted by topic author sza

Short answer: You want to dynamically allocate the matrix given your row/column parameters.

 

Long answer: Dynamic allocation of 2D matrices is a bit of a tricky subject in C (especially since you don't know anything about what the CVI function expects in terms of memory structure for the array).  If CVI is using an optimized assembly language matrix operation that expects a statically allocated matrix, then you might have to fake the subscripting and "flatten" your 2D matrix.  If you want all of the details, you should check out:

http://c-faq.com/aryptr/dynmuldimary.html

 

If you turn on C99 extensions in CVI, you might be able to take advantage of variable length arrays by allocating the desired array at run time.  I belive In memory, this will look like a standard static matrix structure, although I don't have a lot of experience with C99 VLAs.

0 Kudos
Message 2 of 3
(3,605 Views)
Thank you!  I upgraded to LabWindows 9.0, turned on 'Build with C99 extensions' (Options -->  Build Options) and it works!
0 Kudos
Message 3 of 3
(3,603 Views)