Get average y value per x position in Matlab
matlab
Solution
You can use the histogramming function `histc` to get each of the categories:
x = [ 1 4 4 5 5 5];
y = [ 5 1 3 3 4 5];
xs = unique(x);
[frequencies xb] = histc(x, xs); % counts the number of each unique occurrence
ysp = sparse(1:numel(x), xb, y); % a sparse matrix is a good way to organize the numbers
ys = full(sum(ysp)./sum(ysp>0)); % each column in the matrix corresponds to a "index"
This gives you the three arrays you wanted. I think this is quite clean and efficient - no looping, only four lines of code.
Problem
I don't know how to phrase this, but an example: ``` x = [1 4 4 5 5 5]; y = [5 1 3 3 4 5]; ``` and then I'd like output ``` xs = [1 4 5]; ys = [5 2 4]; frequencies = [1 2 3] ``` (because the average 'y' at x=1 is `5`, and the average 'y' at x=4 is `(1+3)/2 = 2`, and the average 'y' at x=5 is `(3+4+5)/3 = 4`). I can compute this in a clumsy way but maybe there's a nice solution.