03-17-2009 11:20 AM
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!
Solved! Go to Solution.
03-17-2009 02:54 PM
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.
03-17-2009 03:06 PM