LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How best to insert a sub-array (row) into a 2D array at the correct index which is depending on the value of the first column

Solved!
Go to solution

Hi

 

I would like to insert a sub-array (row) into a 2D array at the correct index. The position ie index value is depended on the value of the first column of the 2d array.

As an examlple my 2d array would look like this

230   50   215   255

300   60   270   330

360   20   350   370

 

And I would like to insert another row (sub-array) with the following values

 

320   40   300   340

 

This new row should be placed between the second and third row (it is depending on the first column only).

I have tried the Threshold 1D array function by taking a 1Dsub-array from my 2d array (first column) and then using the first value of the new row (320) as a threshold. It sort of work but it does not work when I start the array (ie there is only 1 row) and it seems to not work correctly on other occasions (as explained in the Labview help).

 

Hope the explanation is clear enough for any suggestions. Thank in advance for the help!

0 Kudos
Message 1 of 22
(11,567 Views)

JTRI, please look at the attached example.

You will need to compare the first column of your subarray to the first column's element in each row.

* Note: this will only work if your array is sorted as you have shown in your example.

Is this always going to be the case?

Or is that what you wanted this function for in the first place?

Cory K
0 Kudos
Message 2 of 22
(11,560 Views)

You could use the Threshold 1D Array primitive to do the lookup for you.

 

21760i2C41C66DF16A3BAD

 

 

 

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 3 of 22
(11,547 Views)

Hi 

 

Thanks for the quick reply! I tried to open your VI but I am working with version 8.2 so it did not open.

Can you please repost with the VI saved for a previous version (8.2)or post it as an image?

Your solution sounds great and seems along the lines of what I am trying to do. 

 

The idea is that I start with a new array and add these rows in the right sequence every time the user defines the values (including the first column) I would like to sort them 'actively' / 'in real time' rather then gather all the data and then sort it. If the indexing works well from the start then the 2D array should always be in the right order (that is the exact idea).

 

I have tried use the Threshold function but it somehow falls over because I am starting with an empty array ie it can not work when the 2D array has only one value in it.

 

Many thanks

 

Jack

 

0 Kudos
Message 4 of 22
(11,539 Views)
Solution
Accepted by topic author JTRI

 


@JTRI wrote:

 

The idea is that I start with a new array and add these rows in the right sequence every time the user defines the values Jack

 


 

Ahh, then try this.

This will work with an empty array too.

What you will want to do with this function is make it a subVI.

Make the 'array' and 'subarray' inputs on the connector, then 'new array' an output.

 

You can then put this subVI in a loop with a shift register and it will help add all the new rows in sorted order as they are added.

Is that what you were shooting for?

Cory K
Message 6 of 22
(11,524 Views)

If you don't mind sorting every time you add a row, the OpenG Sort Array will do what you want, I think.

21766iA51245063691C6DC

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 7 of 22
(11,521 Views)

Hi again

 

Thanks Cory K that seems to do the trick. I will have a play with it to make sure it does not fall over but so far so good!

Excellent!

 

Regards

 

Jack

0 Kudos
Message 8 of 22
(11,510 Views)

I seem to have a similar problem, but my data is in a different format i.e.

 

 Col1          Col2       Col3      Col4

"Testing     Testing       0           0

 Testing     Testing       4           1

 Testing     Testing       7           0

 Testing     Testing       12         2

 Testing     Testing       14         1"

 

From above 2D array, Column 1 & Column 2 represent Data, but column 3 and column 4 represent Row and Column respectively.

Basically numbers on col 3 & 4 indicate where col 1 & 2 should be placed in a 2D arra, and for the missing rows where there is no data, it should be padded with Empty arrays.

 

This is more of an "Idea" problem that I'm having rather than programming. Any help is appriciated.

 

Thanks

 

Kas

0 Kudos
Message 9 of 22
(10,963 Views)

Hi Kas,

 

1) get the max of column 3&4 to Initialize an empty array

2) now loop over the rows of data, use values from column 3&4 as indices for replacing elements of array from step 1 with values from current row...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 10 of 22
(10,947 Views)