1

I have a case like, I have to fetch the non zero values of an array, sort them and find the median of values. I did as follows,

var array2 = (from t in array1 where t.array2 != 0 select t.array2).ToArray();
Array.Sort(array2);
var Median = array2.Length % 2 == 0 ? new List<double>() { ((array2[array2.Length / 2 - 1]) + (array2[array2.Length / 2])) / 2 } : new List<double>() { array2[array2.Length / 2] };

For now all going fine. But I am thinking of merging the first two lines in a single one like, copying the non zero values using orderby method. For this I have tried like,

var array2 = (from t in ipedsTableValue where t.array2 != 0 select t.array2).ToArray(t); // Not worked

Also suggest me if there are any super way to calculate median of an array in C# because that line is also looking so big and I am afraid that is not easily readable.

I have referred SO resource: Add a Median Method to a List Though it is working fine, I am thinking of some simple and efficient code with some less number of codes. Any suggestion would be helpful!

2
  • Possible duplicate of Add a Median Method to a List Commented Oct 19, 2016 at 8:24
  • Modified your initial line, to suggest that you want to fetch non zero values in an array, sort them (which is required for a median) and calculate Median. It was misleading to suggest that you need to copy to another array, which caused the initial discrepancy in my first solution. Commented Oct 19, 2016 at 14:17

4 Answers 4

1

You can use linq and a conditional operator.

var filterd = array1
    .Where(x => x.array2 != 0)
    .Select(x => x.array2)
    .OrderBy(x => x)
    .ToArray();

var length = filterd.Length;

var median = length % 2 == 0
    ? (filterd[length / 2 - 1] + filterd[length / 2]) / 2.0
    : filterd[length / 2];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply, I am getting error like, Operator '!=' cannot be applied to operands of type I used.
1

It should look something like that

var median = array2.Take((array2.Length%2 == 0) ? ((array2.Length/2) + 1) : ((array2.Length + 1)/2))
                    .Reverse()
                    .Take((array2.Length%2 == 0) ? 2 : 1)
                    .Sum(x => x);

Comments

1

Array copy and ordering operation:

var filteredArray = array1.Where(x => x != 0.0).OrderBy(x => x).ToArray();

Calculate Median

double midpoint = (filteredArray.Count() - 1) / 2.0;
double median = (filteredArray[(int)(midpoint)] + filteredArray[(int)(midpoint + 0.5)]) / 2.0;

1 Comment

I misunderstood your initial requirement as copying all the elements to another array except 0th element, I have now modified to remove the 0.0 values and calculated median
0

Try like this

var array2= (from t in array1 where t.array2!= 0 select t.array2).OrderBy(t => t).ToArray();

2 Comments

That's amazing. Thanks TomFarCry
Where's median calculation ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.