LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

All diagonals of a Matrix

Solved!
Go to solution

hello! I am actually looking to find all diagonals of a  matrix.In the attached file, i m trying to shuffle column every time while rows remain constant so the first element must be from row num:01, then find other elements from that refrence.The problem i m facing right now is that i m not getting vectors with [diagonal 1, diagonal 2, diagonal3.....]i get only last diagonal with   2 elements . I f i places indicator outside the loop, it make it 3D, however output remains same and i m unable to segregate all diagonals as independent vectors.At one iteration it should give all elements as vector then on other iteration another vector, but i want to process independetly later on.So for 4X4 Matrix there are five diagonals which leads to 5 vectors i  want......they all are 1-D.Hope i managed to convey the issue.Kindlt see vi which i have attached.

0 Kudos
Message 1 of 4
(5,452 Views)

The way you have the for loop wired, you only get the values from the last iteration (i=2). If you read the detailed help for Get Matrix Diagonal, it tells you that the output is a 2D array with only one column of data.  If you index out that column and then feed that out of your for lopo through an auto indexing tunnel, you should get the three diagonals starting in the top row. To get the other two, you need to change the row index.

 

Note that in LV all rows in a 2D array must be of equal length and all columns must be of equal length (although the rows need not have the same length as columns). So you will get default values (zero) at the ends of shorter diagonals.  If zero is a valid element value in your array, you may need to account for this.

 

Lynn 

0 Kudos
Message 2 of 4
(5,449 Views)

Hello!

I have made a logic and it actually works for the case of 4x4 matrix but i want to generalize for the case of any size of matrix.I have added a case structure in which  case is controlled by iteration i.That i actually controls the column or row of the Get Matrix diagonal.I want to generalize but not having any solution.Kindly see the vi in attachment.

0 Kudos
Message 3 of 4
(5,420 Views)
Solution
Accepted by topic author sets

I looked at the problem somewhat differently.  The number of diagonals is given by the number of columns minus 1 plus the number of rows minus 2. Further, the indexes to be applied to the Get Matrix Diagonal function have a consistent pattern, regardless of the size of the array. That pattern can be generated by adding or subtracting 1 to the appropriate row or column index without changing the program for each size of array.

 

I presented the output as an array of clusters of array.  By putting each diagonal array into a cluster and then building an array of the clusters, the individual diagonal arrays can have different lengths.  Because arrays in LV are arbitrary length, you do not need to create an indicator for each possible diagonal.  I ran my VI with 24 rows and 55 columns without changing anything but the values in the input size controls.

 

I also added a delay in each iteration.  Running thousands of iterations per second is rather useless.

 

Lynn

Message 4 of 4
(5,401 Views)