duplicate point issue with using griddata
조회 수: 54 (최근 30일)
이전 댓글 표시
Hello,
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
Muhammad Atif
2021년 9월 4일
Hi Ian,
I am having same issue. Have you found any solution and could you please advise me.
thanks
답변 (2개)
Walter Roberson
2021년 9월 5일
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);
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
ty = y + randn(size(y))/100000
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.
댓글 수: 0
Muhammad Atif
2021년 9월 5일
편집: Walter Roberson
2021년 9월 5일
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..
Thanks
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!