median

Complexity of finding the median using 2 heaps

ぃ、小莉子 提交于 2019-12-12 09:49:59
问题 A way of finding the median of a given set of n numbers is to distribute them among 2 heaps. 1 is a max-heap containing the lower n/2 (ceil(n/2)) numbers and a min-heap containing the rest. If maintained in this way the median is the max of the first heap (along with the min of the second heap if n is even). Here's my c++ code that does this: priority_queue<int, vector<int> > left; priority_queue<int,vector<int>, greater<int> > right; cin>>n; //n= number of items for (int i=0;i<n;i++) { cin>

R Programming; Calculate median of row using FOR loop

和自甴很熟 提交于 2019-12-12 04:16:02
问题 How would I calculate the median (use the median function) of each row by using the FOR loop. Using a matrix; mat = matrix(rnorm(100), 5) Thank you in advance. -Bill 回答1: Here is one possible solution: mat = matrix(rnorm(100), 5) medians_of_mat <- numeric() for(i in 1:nrow(mat)) { medians_of_mat[i] <- median(mat[i, ]) } An easier way would be to use apply: apply(mat, 1, median) 回答2: We could use rowMedians from library(matrixStats) library(matrixStats) rowMedians(mat) 来源: https:/

Expanding a Frequency Table Where the Variable Names are the Values

家住魔仙堡 提交于 2019-12-12 02:45:04
问题 I am working with a dataframe where each observation is linked to a specific ID, and I have a set of variables that define the "values" as if I had a factor variable. However, the value in the "cell" is the frequency. Here is a simplified version: ID 1 2 3 A 2 3 2 B 1 4 1 I would like to get two vectors that expand the frequencies so that I can calculate an interpolated median for each ID. That is, I'd like something of the form: A B 1 1 1 2 2 2 2 2 2 2 3 3 3 The psych package has a function

Compare and keep character observations that are the same

偶尔善良 提交于 2019-12-12 00:07:19
问题 I have the following data set: Date ID Company Jan05 1 Coca-Cola Jan05 2 Coca-Cola Jan05 3 Coca-Cola Jan05 4 Apple Jan05 5 Apple Jan05 6 Apple Jan05 7 Microsoft Feb05 1 McDonald Feb05 2 McDonald Feb05 3 McDonald Feb05 4 McDonald Feb05 5 McDonald Feb05 6 Microsoft . . . Jan06 1 Apple Jan06 2 Apple Jan06 3 Apple Jan06 4 Apple Jan06 5 Apple Jan06 6 Apple Jan06 7 Apple Feb06 1 McDonald Feb06 2 McDonald Feb06 3 McDonald Feb06 4 McDonald Feb06 5 McDonald Feb06 6 Lenova Feb06 7 Lenova . . Jan07 1

Calculating Geometric median of 2D points

流过昼夜 提交于 2019-12-11 17:53:31
问题 I am calculating Geometric median of some (x,y) points in java. To calculate Geometric median , first i am calculating centroid of all the points, then this centroid is used to calculate Geometric median . My code works fine, but sometimes it goes to an infinite loop (i think.). The problem is with my while condition. This while condition should be change according to input points, but i don't know how. Below I am putting the complete code. import java.util.ArrayList; public class

How to finish code to replace NA with median in R

a 夏天 提交于 2019-12-11 15:56:28
问题 I am very new to R, so please please be gentle. I am working on the Kaggle Titanic competition, to get me into R and working things out. I am working my way through engineering a feature and I am a bit stuck with the logic of what to do next. So, here goes. My goal is to take the Age data and replace all of the NA with the median of age for the title of the person. e.g. if the person is a master, I want to get the median of all the masters and replace the NA with that median. Same for Mr. and

Finding Median between TWO dates SQL Server 2008

余生颓废 提交于 2019-12-11 14:54:31
问题 I am looking of a way to take the MEDIAN of a bunch of start and end dates (LOTS AND LOTS of dates). However, it would be specific to various "invoice numbers." See sample data below. invoice_no invoice start date invoice end date 4006 11/14/2001 12/15/2004 20071 11/29/2001 02/01/2003 19893 11/30/2001 12/02/2001 19894 11/30/2001 12/04/2001 004 10/22/2002 10/31/2002 004 12/02/2002 10/31/2002 004 01/19/2002 10/31/2002 004 05/10/2002 10/31/2002 Find median between start and end date. For an

Microsoft Office Access - Median function - Too few parameters

巧了我就是萌 提交于 2019-12-11 14:40:58
问题 I am trying to use this code to calculate median from my query which has these criteria: <[Form]![testForm2]![crit1] And >[Form]![testForm2]![crit2] and <[Form]![testForm2]![Age1] And >[Form]![testForm2]![Age2] without these criteria function works well and gives for every task median based on "MP", however when I put in there my criteria I receive error: error - Too few parameters. Expected 4 and then it says 'Object Variable or With block not set' my input: DMedian("MP";"testForm2";"[TASK]=

Element-wise median of a lot of matrices, python pandas

廉价感情. 提交于 2019-12-11 11:18:31
问题 I have a dictionary of matrices. The dictionary is called dict. dict[location] returns a square n x n correlation dataframe for that location. locations is the list of all locations. (Keys in the dictionary). I want to essentially make a list of every i,j component in a dataframe across keys and take the median of all of those. You can think of this as stacking the matrices on top of each other and taking the median value for each i,j element. I hope I explained this clearly enough. I was

Median of arbitrary datapoint around index - MATLAB

旧街凉风 提交于 2019-12-11 10:03:40
问题 I've been using the findpeaks function with great success to detect peaks in my signal. My next step is to clean these identified peaks, for which I have the indices. My goal is to calculate the median of Y data points before and Y data points after a given index and replace whatever values (noise) there are with these new values (the calculated median). Something like this: % points before, peak, points after % ↓ ↓ ↓ x = [1, 2, 3, 1, 34, 3, 2, 1, 3] Calculate the median of the 4 data points