10-21-2011 08:35 AM
Hi,
I need a little insight on this.....
If I have a 1D array of integer values, what woudl be the easiest way to tally up the quantites of same numbers (not addition)?
For example, my 1D array might be this: 1,6,4,55,23,6,2,55,23,23
I would like to report the quantites in 2D array (or not an array, whatever is easiest):
1 1
6 2
4 1
55 2
23 3
I'm not sure how to do this simply.....I see that search array gives the element number and only the first element it sees with that value. I just need pointed in the right direction.
Thanks,
SJ
Solved! Go to Solution.
10-21-2011 09:16 AM
There is a certain amount of complexity involved with solving this. I would do it as follows.
At the end, you will have a value array containing all the values and a count array containing the counts at each value. Note that this particular algorithm is straightforward, but will be slow for huge arrays. See this post for inspiration on how to speed it up (a lot).
10-21-2011 11:07 AM - edited 10-21-2011 11:08 AM
here are two different ways to do it.
The top one uses the Histogram function to count the number of elements in bins of size=1. (then you have to go through and clean up the arrays to eliminate empty bins.)
The botom solution is similar to the comment above. It uses the 'Search 1D array' to determine if an element already is in the array. If the element is there, the Count of the element is increased by one. If it is not there, it is added to the array with a count of 1.
10-21-2011 11:18 AM
Dear Jack. Please find the attached vi and image to solve your problem..
Have a good day..
10-21-2011 12:06 PM
I would try something like this:
Sort & Reverse array. Start at last element, search for it from the beginning. Difference between locations gives number of elements. Jump to next value and repeat as necessary. I have used the built-in primitive to search the 1D array, since I spent the effort to sort the array I would normally use my personal search using bisection which can be much faster. An excuse to agitate again for the following:
10-24-2011 01:14 PM
Sazi,
This worked great. This is exactly what I was looking to do.I never thought of trying this approach. Thank you very much.
Thanks everyone for the great ideas. Much appreciated.
Ryan
10-24-2011 01:47 PM - edited 10-24-2011 01:48 PM
@SimpleJack wrote:
I would like to report the quantites in 2D array (or not an array, whatever is easiest):
If the elements are non-negative integers and in a relatively narrow range, you could just create a simple 1D array where the index is the number and the value is the count. Here is an example where the result is shown on a histogram. (I also attached a snippet)
10-24-2011 02:40 PM - edited 10-24-2011 02:41 PM
Here's a simple version that does the 2D array output as describved in the original question. Try it!
10-24-2011 03:04 PM - edited 10-24-2011 03:04 PM
@Sazi wrote:
Dear Jack. Please find the attached vi and image to solve your problem..
I am wondering what would happen of you remove the "equal false" and swap the inputs to the select node, even eliminating a crossed wire. 😄
An "equal false" is just an invert, right? See also this long thread... ;))
10-24-2011 03:09 PM