LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

The element in the middle of the array

plz any suggestion how to solve this :

If the array has an even number of elements, then use (N/2) as the middle element. For example, if the array has
ten elements then the middle-most would be (10/2) = 5 or the 5th element in the array. If the array has an odd number of elements, then the middle will be (N/2) + 0.5. For example, if the array has
seven elements then the middle-most would be (7/2) + 0.5 = 4 or the 4th element in the array.

0 Kudos
Message 1 of 8
(7,140 Views)

See if this meets your requirements.

 

The Ramp Pattern stuff was just to make an easy array to verify the rest of the code worked.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 8
(7,120 Views)

Hi jiji,

 


@jiji_830 wrote:
  1. If the array has an even number of elements, then use (N/2) as the middle element. For example, if the array has
    ten elements then the middle-most would be (10/2) = 5 or the 5th element in the array.
  2. If the array has an odd number of elements, then the middle will be (N/2) + 0.5. For example, if the array has
    seven elements then the middle-most would be (7/2) + 0.5 = 4 or the 4th element in the array.

  1. When the array has an even number of elements then there is no "middle" element! Example: in an array of 4 elements [1 2 3 4] you will pick either the 2nd or the 3rd element as "middle". Your calculation "N/2" would constantly pick the element before the "middle"…
  2. When the array has an uneven number of elements then there will be a clear "middle" element - as noted by your calculation…
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 3 of 8
(7,097 Views)

I'm not sure where you are having problems -- with the concept of "middle of the array", with the "decision-making" that is described in the algorithm you posted, or "how to program" such an algorithm.

 

Personally, I think the biggest issue is "What is the middle element of a discrete ordered set of data"?  If I hold up three finger, I think all reasonable people will agree that the second finger, with one finger on either side, is the "middle" finger.  What if I hold up 4 fingers?  Is it the second?  Is it the third?  What is meant by "middle"?

 

In statistics, there is the concept of the "mean", or "average".  If I number my fingers 1, 2, 3, ..., and take the mean of the 3 finger numbers, I'll compute (1+2+3)/3 = 6/3 = 2.  If I do this for only 2 fingers, I will compute (1+2)/2 = 1.5, which is exactly half-way between 1 and 2.  But if I restrict myself to integers (because I want to identify the "middle finger"), I am forced to use an integer, so I "adopt a convention" and round up, or round down (depending on what convention I choose).

 

Let's number the elements (or "fingers") starting with 1 and numbering 1, 2, 3, ...  How do we find the "average" (= "middle") value?  Simply sum the series and divide by the number of entries.  Do you remember from High School Algebra the formula for 1+2+...+n?  It is n*(n+1)/2.  If we divide by n, we get the "middle" as (n+1)/2, which is an Integer if n is odd.

 

Sometimes the more interesting questions are the ones that don't get asked ...

 

Bob Schor

0 Kudos
Message 4 of 8
(7,083 Views)

@jiji_830 wrote:

plz any suggestion how to solve this :

If the array has an even number of elements, then use (N/2) as the middle element. For example, if the array has
ten elements then the middle-most would be (10/2) = 5 or the 5th element in the array. If the array has an odd number of elements, then the middle will be (N/2) + 0.5. For example, if the array has
seven elements then the middle-most would be (7/2) + 0.5 = 4 or the 4th element in the array.


You are making a sequence of incorrect statements and I am not sure what you consider "solve this". (implement the faulty math in LabVIEW? Come up with better definitions?)

 

As others have said, a 1D array with an even number of elements has no middle element, but you can of course do a linear interpolation between the two values adjacent to the middle. A 1D array with an odd number of elements has a clear middle. In all cases, the indices of the interesting element can be calculated from the array size.

0 Kudos
Message 5 of 8
(7,080 Views)

I feel like this was a school assignment and the problem statement was copy-pasted. Such that the "middle" element here is defined exactly how the problem statement says, much to all of our dismay.

Redhawk
Test Engineer at Moog Inc.

Saying "Thanks that fixed it" or "Thanks that answers my question" and not giving a Kudo or Marked Solution, is like telling your waiter they did a great job and not leaving a tip. Please, tip your waiters.

0 Kudos
Message 6 of 8
(7,073 Views)

"Interpolate array" will correctly interpolate for fractional indices, so this will give the value of the middle element (size=odd), or the average of the two elements adjacent to the middle (size=even).

 

altenbach_1-1634581073429.png

 

0 Kudos
Message 7 of 8
(7,071 Views)

thank u all for ur clarification. i miss understanding the problem now I got an idea. i will fixe it. thank u all so much 

0 Kudos
Message 8 of 8
(7,063 Views)