LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to check if we have selected duplicated value from random generator?

Solved!
Go to solution

Hi all, I'm new in LabVIEW and I got a task on randomly select one element from the 2D array.

The VI should:

1) Open a file to get a 2D array.

2) Randomly select one element from the 2D array.

3) The process is repeat but must not select the same element previously selected.

4) Repeat process (3) for M/2 times, where M is total number of element in the 2D array.

 

The process flow:
1) Read a spreadheet file.
2) Check number of array element as M.
3) Calculate looping number as M/2.
4) Generate random row index and column index as a pair of coordinate.
5) Keep a history record of the row and column index pair.
6) Check if this current index pair is previously generated.
If yes, discard and regenerate.
If no, index the element from the 2D array.
7) Loop for M/2 times.
😎 Record the values of all selected elements as a new 1D array, write it into a spreadsheet file with new name.

 

My problem start when I have to checked if the element is selected or not. I created case structures to check the element but I don't know if it is relevant or functioning cause the program cannot run. I also attached the file. Any kind of help is really appreciated. Thank you.

0 Kudos
Message 1 of 6
(2,943 Views)

There is a lot of code there, and I think a lot more than you actually need.  I'm not going to dig into it.

 

A couple of quick tips.

1.  Index Array is resizable.  Drag down the bottom border.  You'll get indices 0 and 1 by default without wiring up constants.

2.  If you split a wire and it is used 3 places inside a For Loop.  Put the splits inside the For Loop.  No need to create 3 tunnels on the For Loop when they all reference the same value when 1 tunnel will do.

 

For a tip on your original question.  If you have a 2-D array of elements you don't want to repeat, then also create a 2-D array of booleans initialized with False.  When you randomly select an index, change that value in the Boolean array to True.  If you later detect the element is True, you know you need to grab another, if it is False, you know the element is unused.

 

Look at the Riffle function.  It takes a 1-D array and randomly shuffles the elements.  Then you can just index through that array and you'll know each element is in a random position and only once.  Now you talk about a 2-D array,  I don't know what is the physical meaning behind having a 2-D array vs. a 1-D array in your situation.  But you could reshape the 2-D array into a 1-D array.  Riffle that, then reshape the array back into a 2-D array.

Message 2 of 6
(2,938 Views)

If you click the run arrow, it'll tell you that N's of FOR loops and the termination condition of while loops are not wired.

 

All you probably need to do is create an integer array containing the number of array elements (MxN), feed it through riffle array, take the first half, and use Q&R to get the random 2D indices. See how far you get. Shouldn't take code more than the size of a postage stamp.

Message 3 of 6
(2,936 Views)
Solution
Accepted by topic author activate_creaty

@altenbach wrote:

 

All you probably need to do is create an integer array containing the number of array elements (MxN), feed it through riffle array, take the first half, and use Q&R to get the random 2D indices. See how far you get. Shouldn't take code more than the size of a postage stamp.


 

Here's what I had in mind (check for bugs and modify as needed. This is just a very rough draft. Maybe some things need to be swapped)

 

PickPixels.png

 

 

(note that I tap into the "riffle index" output for simplicity)

 

Message 4 of 6
(2,927 Views)

The above solution is most universal, because it works with any array, even if the elements are complicated data structures.

 

For numeric arrays as in this case, you can even go loop-free:

 

PickPixelsLoopFree.png

0 Kudos
Message 5 of 6
(2,916 Views)

Thanks a bunch! The file work just fine and I just replaced the "input image" to "read spreadsheet" as I need to read the data from excel spreadsheet.  This really help me. Thanks 🙂


@altenbach wrote:

@altenbach wrote:

 

All you probably need to do is create an integer array containing the number of array elements (MxN), feed it through riffle array, take the first half, and use Q&R to get the random 2D indices. See how far you get. Shouldn't take code more than the size of a postage stamp.


 

Here's what I had in mind (check for bugs and modify as needed. This is just a very rough draft. Maybe some things need to be swapped)

 

PickPixels.png

 

 

(note that I tap into the "riffle index" output for simplicity)

 


 

0 Kudos
Message 6 of 6
(2,877 Views)