Clear Filters
Clear Filters

duplicate point issue with using griddata

49 views (last 30 days)
Ian James
Ian James on 21 Sep 2011
Edited: Walter Roberson on 5 Sep 2021
I'm trying to use griddata function from Matlab but I get an error that says that duplicate x-y points are detected. This is how I tried to remove the duplicate points.
For example; I have
x = [1 2 2 3 4]
y = [0 1 2 3 3]
z = [0.1 0.2 0.3 0.4 0.1]
Since the 3rd entry in x is repeated I remove it, and I also the third entry from both y and z vectors (since z is a function of both x and y).
And, the last entry in y is also repeated therefore I delete the last entries of all the vectors. I end up getting,
x = [1 2 3]
y = [0 1 3]
z = [0.1 0.2 0.4]
I used unique function of matlab to do this, for example
[u_x,i,j] = unique(x,'first'); u_y = y(i); u_z = z(i);
[u_y2,iy,jy] = unique (u_y,'first'); u_x2 = u_x(iy); u_z2 = u_z(iy);
But I still get the same error that there are duplicate points.
Could someone please explain to me what I'm doing wrong? THanks.
  1 Comment
Muhammad Atif
Muhammad Atif on 4 Sep 2021
Hi Ian,
I am having same issue. Have you found any solution and could you please advise me.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 5 Sep 2021
It is not an error to have duplicate x coordinates for griddata(): it is only an error to have locations in which the x and y are both the same as other locations. And that is a warning rather than an error.
x = [1 2 2 3 4];
y = [0 1 2 3 3];
z = [0.1 0.2 0.3 0.4 0.1];
[XQ, YQ] = meshgrid(1:.5:4, 0:.5:3);
ZQ = griddata(x, y, z, XQ, YQ);
surf(XQ, YQ, ZQ, 'edgecolor', 'none')
x(3) = x(2); y(3) = y(2);
ZQ = griddata(x, y, z, XQ, YQ);
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
surf(XQ, YQ, ZQ, 'edgecolor', 'none')
You can do things like
[UXY, aix] = unique([x(:), y(:)], 'rows');
ux = UXY(:,1); uy = UXY(:,2); uz = z(aix);
ZQ = griddata(ux, uy, uz, XQ, YQ);
surf(XQ, YQ, ZQ, 'edgecolor', 'none')
What this has effectively done is removed the z information for the second and subsequent locations at which x and y were identical to other locations. But you have to decide whether that is appropriate.
Sometimes what people do is add a small random component:
format long g
tx = x + randn(size(x))/100000
tx = 1×5
1.00000265625759 1.99999976138769 2.00001020376416 2.99999366902687 4.00000834420059
ty = y + randn(size(y))/100000
ty = 1×5
-2.37898280301613e-06 0.999981074451582 0.999993721740802 3.00001259860834 3.00000410757718
ZQ = griddata(tx, ty, z, XQ, YQ);
surf(XQ, YQ, ZQ, 'edgecolor', 'none')
However you have to be careful with this: the randomness might push some or all of your query points to be outside of the area defined by the modified points, and griddata() does not offer any extrapolation method. scatteredInterpolant() on the other hand does offer extrapolation.

Muhammad Atif
Muhammad Atif on 5 Sep 2021
Edited: Walter Roberson on 5 Sep 2021
Hi Walter,
Thank you so much for sharing a detailed example. I would highly appriciate if you could provide any sugggestion on my question. I have used unique to remove duplicate data points and I am still facing this warning but I am not concerned with warning. however, at the end I am getting weird contour shape. Please visit this question for detail pictures and data..


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!