LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Find All Matching Elements in a 2D array

Solved!
Go to solution

I'm given a 2D Array of strings where each row indicates two values are connected for example:

{A,B;
 G,'';
 D,C;
 D,A;
 E,F;
C,B;
H,''}

 

Indicates that:

  • A, B, C and D are connected
  • E and F are connected
  • G and H are orphans

From that given input, I would like to make a VI that would output the array

 

{A,B,C,D;
 E,F,'','';
G,'','','';
H,'','',''}

 

I am using the open G toolkit and currently, I have this solution:

 

2D-Pair.png

 

As this is a fairly critical vi I would appreciate a review to:

  1. Make sure I didn't miss an edge case (It is safe to assume that the input array will always be NX2 and 'A' = '\sA'='A\s')
  2. Suggest any changes I could make to improve the Vi's scope or efficiency.

 

0 Kudos
Message 1 of 14
(3,308 Views)

Sorry, I don't have any OGTK installed, so I cannot run your example. I would think a variant attribute based solution might be simpler. Have you tried?

0 Kudos
Message 2 of 14
(3,273 Views)
Solution
Accepted by topic author ATE-EGNE

@altenbach wrote:

I would think a variant attribute based solution might be simpler. Have you tried?


See if this works for you. 🙂 (No guarantees. Can probably be further simplified).

 

VariantGrouping.png

Message 3 of 14
(3,258 Views)

The following "breaks" your output (though the algorithm seems to work, it's just the Output that is strange) -- add another pair, (A, E), to your Input Array.  Before running this, predict the output (you'll probably be wrong).

 

Bob Schor

Message 4 of 14
(3,235 Views)

Why is the output strange? I do not understand. A is connected to E, E is connected to F, thus A and F are connected by E.

 

I guess it depends on how you define a match, it appears here anything connected is considered a match.

 

It can be different depending on your matching criterion. Assume you are matching everything that is 10% within each other and you have the following:

A = 0.9

B = 1

C = 1.1

 

B & C are within 10% of each other, B is within 10% of A but A is not within 10% of B, so do they match? Much harder to match and get groups. (Sidenote: I tried to do something similar with lengths of DNA fragments and found matches with Mathematica, much more code than CA's solution 🙂  which I am still trying to understand completely.)

 

mcduff

0 Kudos
Message 5 of 14
(3,229 Views)

@Bob_Schor wrote:

The following "breaks" your output


Who is "your"?

0 Kudos
Message 6 of 14
(3,224 Views)

@altenbach wrote:

@Bob_Schor wrote:

The following "breaks" your output


Who is "your"?


I meant the Original Poster.  I understood the format for output was strings (arrays?) listing the "connected elements", with possibly spaces at the end.  By adding an additional input element that resulted in "merging" two strings, I ended up with an output that seemed to be in the "wrong" format.  Since the Point of the Exercise, I thought, was to have a nice algorithm that worked, rather than try to show that there were no "edge cases" that the OP missed (which is hard, in general), I demonstrated (I thought) the existence of an Edge Case, proving his algorithm was faulty.  Unless I downloaded it wrong ...  or my version of LabVIEW isn't working.

 

Bob Schor

Message 7 of 14
(3,219 Views)

@Bob_Schor wrote:

The following "breaks" your output (though the algorithm seems to work, it's just the Output that is strange) -- add another pair, (A, E), to your Input Array.  Before running this, predict the output (you'll probably be wrong).

 

Bob Schor


You are correct! I think I'll try to fix this, but I'll end up going with @altenbach's solution. I did not know you could do that kind of grouping.

Message 8 of 14
(3,193 Views)

@altenbach wrote:

@altenbach wrote:

I would think a variant attribute based solution might be simpler. Have you tried?


See if this works for you. 🙂 (No guarantees. Can probably be further simplified).

 

VariantGrouping.png


I didn't even know you could do this! Also, if I'm not mistaken this vi would work for an N1xN2 array as long as N2max was known at compile time.

 

I'd be interested to know if this could be optimized further and debugged. I think I would like to use this logic as it seems to be faster and more versatile.

0 Kudos
Message 9 of 14
(3,187 Views)

@mcduff wrote:

Why is the output strange? I do not understand. A is connected to E, E is connected to F, thus A and F are connected by E.

 

I guess it depends on how you define a match, it appears here anything connected is considered a match.

 

It can be different depending on your matching criterion. Assume you are matching everything that is 10% within each other and you have the following:

A = 0.9

B = 1

C = 1.1

 

B & C are within 10% of each other, B is within 10% of A but A is not within 10% of B, so do they match? Much harder to match and get groups. (Sidenote: I tried to do something similar with lengths of DNA fragments and found matches with Mathematica, much more code than CA's solution 🙂  which I am still trying to understand completely.)

 

mcduff


That's an interesting problem, fortunately for me, being connected is a boolean operation. the comment about the breakage was due to the fact that adding

{'A','E'}

at the end would produce

{'','','A','B','C','D','E','F'}

 Giving me two phantom connection points

0 Kudos
Message 10 of 14
(3,185 Views)