LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Transpose Function

I  have a question about the Advanced analysis pack 'Transpose' function.
 
I want to transpose a NxN matrix into a new matrix. Simple enough, but I am using double pointers (**) and the Transpose function doesn't seem to like this.  I couldn't find any documentation on the exact way that this function is supposed to be used - other than a 2D array should be passed in - but here is what I have found through experimentation.  I am looking for an applications engineer here to tell me that what I am doing is OK and won't end up breaking my program due to some obscure memory allocation error somewhere down the line.
 
1.) passing an array such as **foo into the 'Input Matrix' parameter will cause the function to fail.
2.) passing an array such as foo[N][N] into the 'Input Matrix' paramater will allow the function to succeede.
3.) passing in an array such as *foo with the elements arranged as [row1],[col1],[col2],[colN]...[rowN],[col1],[col2],[colN] into the 'Input Matrix' will allow the function to succeede.
4.) rules 1-3 apply for the 'Output Matrix' parameter as well.
 
I conclude that as long as the arrays coming in and out of the function are set in contigious memory that this will not cause any problems.   So it would be OK to use a single, dynamically sized pointer to pass into these arrays, so long as I do all of the memory allocation outside of the function?  Would this be a true statement?
 
Also, would this apply to all of the Advanced Analysis Matrix functions?
0 Kudos
Message 1 of 2
(3,272 Views)

Dave,

The reason 2) and 3) both work is that the memory layout is the same in both cases. This same behavior should be exhibited by any analysis matrix function. If you want to use a (double **) in your program, I have a suggestion:

Instead of allocating n "row" arrays, simply allocate one array and point into it from your double **.

ex.

int numRows = 10, numCols = 5, i;
double **matrix = malloc(numRows * sizeof(double *));
double *matrixData = malloc(numRows * numCols * sizeof(double));
for (i = 0; i < numRows; ++i)
    matrix[i] = matrixData + i * numCols;

With this setup, you can use matrix[i][j] type notation freely; when you need to pass the array to an analysis function, simply pass (*matrix) which will be what the function expects.

Transpose(*matrix, numRows, numCols, *otherMatrix);

In addition to this approach transitioning easily between these idioms, it only does 2 memory allocations and is therefore faster than doing numRows + 1 allocations. It also makes cleaning up easier, as you only have to free *matrix and matrix; no loop is needed.

Hope this helps...

Regards,

-alex

0 Kudos
Message 2 of 2
(3,262 Views)