help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: deleting duplicities, diag, equidistant data


From: Paul Kienzle
Subject: Re: deleting duplicities, diag, equidistant data
Date: Mon, 28 Feb 2005 19:47:11 -0500

octave-forge has interp1 for interpolating on a regular grid, but
it does not average the y values for repeated x.

If you only have pairs that are duplicated, and your x and y are
already sorted then:

  dups = find(x(1:end-1)==x(2:end));
  x(dups)=[];
  y(dups,:) = (y(dups,:)+y(dups+1,:))/2;
  y(dups+1,:) = [];

Below is a general solution for averaging y values for repeated x
not already ordered based on sort and sparse tricks.

- Paul

function [unique_x,unique_y] = avgdups(x,y)
  [unique_x,i,j]=unique(x(:));
  n=length(x); unique_n=length(unique_x);
  c = columns(y);
  if n != unique_n,
    z=sparse(j*ones(1,c),ones(n,1)*[1:c],y,unique_n,c,'sum');
    nz=sparse(j*ones(1,c),ones(n,1)*[1:c],1,unique_n,c,'sum');
    unique_y = full(z)./full(nz);
  else
    unique_y = y(i,:);
  endif
end

%!test
%! x=[3.2,1.5,2.6,3.2,2.6,7.8]';
%! y=[  3,  4,  2,  5,  1,  1;
%!     10, 12,  8, 12,  5,  9]';
%! [x,y]=avgdups(x,y);
%! assert(x,     [1.5,2.6,3.2,7.8]');
%! assert(y(:,1),[  4,1.5,  4,  1]');
%! assert(y(:,2),[ 12,6.5, 11,  9]');

%!test
%! x=[3.2,1.5,2.6,7.8]';
%! y=[  3,  4,  2,  1;
%!     10, 12,  8,  9]';
%! [x,y]=avgdups(x,y);
%! assert(x,     [1.5,2.6,3.2,7.8]');
%! assert(y(:,1),[  4,  2,  3,  1]');
%! assert(y(:,2),[ 12,  8, 10,  9]');

On Feb 28, 2005, at 3:03 AM, Jiri Pachman wrote:

thanks to all for answers to my previous questions

Hi everybody,
I have this simple problem. During data manipulation I create two (sometimes 3) columns of data. First contains x values the others are y, y2 values. It sometimes happens that I get two values of y are for the same value of x (around 5 couples in matrix of 200 lines). This makes problems in further calculations. I tried gnuplot´s smoothing (smooth spline) function and it works well, taking the duplicate values and making y average for particular x. Unfortunately I do not know, how to extract this data in numeric format from gnuplot. I was looking for something similar in octave, but without luck. Is there something like this in octave?

For now I am using the following script that finds the duplicate values and deletes them, but I would prefer to average them. I also tried to work with diagonal using matlabs diag function, but it seems to work quite differently (that is why I use rather complicated way to change 1 on the diagonal to 0).

x = G12;
vel = rows(x);
indexy = zeros(vel);
for i = 1:vel
ind = x(:,1) == x(i,1);
indexy(:,i) = ind;
diamat(i,i) = 1;
endfor

#% -------- indexes of duplicate values -------------
err = (indexy - diamat);
max(err)

#%--------------------- adding err columns creating index  ---------
ind = err(:,1);
for k=1:vel-1
ind = ind + err(:,k+1);
G12_nove = x(~ind, :);
endfor

I am also interested, if there is some elegant way how to create equidistant data from nonequidistant data. I use polynomial fit and then I calculate the new values in linspace or logspace depending on the need.

thanks for help

Jiri


--
***************************************************
Ing. Jiri Pachman
Katedra teorie a technologie vybusnin
Univerzita Pardubice
Studentska 95
532 10 Pardubice

tel.:  +420-46-603-8018
fax.: +420-46-603-8024
***************************************************



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------




-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]