使用 MATLAB 进行稀疏矩阵插值

发布于 2024-10-17 21:16:00 字数 371 浏览 5 评论 0原文

如果我有一个像这样的矩阵,

A = [1 2; 3 4];

我可以使用 interp2 像这样对其进行插值

newA = interp2(A,2);

,然后得到一个 5x5 插值矩阵。

但是,如果我有一个像这样的矩阵怎么办:

B = zeros(20);
B(3,2) = 5;
B(17,4) = 3;
B(16, 19) = 2.3;
B(5, 18) = 4.5;

我将如何插入(或填充空白)这个矩阵。我研究过 interp2 和 TriScatteredInterp 但这些似乎都不完全符合我的需求。

If I have a matrix like this

A = [1 2; 3 4];

I can use interp2 to interpolate it like this

newA = interp2(A,2);

and I get a 5x5 interpolated matrix.

But what if I have a matrix like this:

B = zeros(20);
B(3,2) = 5;
B(17,4) = 3;
B(16, 19) = 2.3;
B(5, 18) = 4.5;

How would I interpolate (or fill-in the blanks) this matrix. I've looked into interp2 as well as TriScatteredInterp but neither of these seem to fit my needs exactly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晒暮凉 2024-10-24 21:16:00

一个好的解决方案是使用我的 inpaint_nans。只需提供不存在信息的 NaN 元素,然后使用 inpaint_nans。它将对 NaN 元素进行插值,填充它们以与数据点平滑一致。

B = nan(20);
B(3,2) = 5;
B(17,4) = 3;
B(16, 19) = 2.3;
B(5, 18) = 4.5;
Bhat = inpaint_nans(B);

surf(B,'marker','o')
hold on
surf(Bhat)

来自几乎空数组的Inpainted surface

编辑:

对于那些对inpaint_nans是否可以处理更复杂的表面感兴趣的人,我曾经拍过一张数字化的莫奈绘画(见左侧,然后通过删除随机 50% 的像素来破坏它。最后,我应用 inpaint_nans 来查看是否可以很好地恢复图像。右侧图像是修复后的图像。虽然分辨率较低,恢复的图像是一个不错的恢复

Garden at Sainte-Adresse

作为另一个示例,请尝试以下操作:

[x,y] = meshgrid(0:.01:2);
z = sin(3*(x+y.^2)).*cos(2*x - 5*y);
surf(x,y,z)
view(-23,40)

base trig surface

现在,删除该数组中大约 7/8 的元素,并

k = randperm(numel(z));
zcorrupted = z;
zcorrupted(k(1:35000)) = NaN;

使用 z- 进行恢复。轴具有不同的缩放比例,因为边缘周围存在 +/-1 上方和下方的微小变化,但除此之外,后一个表面是一个很好的近似值

zhat = inpaint_nans(zcorrupted);
surf(x,y,zhat)
view(-23,40)

。 " alt="在此输入图像描述">

A good solution is to use my inpaint_nans. Simply supply NaN elements where no information exists, then use inpaint_nans. It will interpolate for the NaN elements, filling them in to be smoothly consistent with the data points.

B = nan(20);
B(3,2) = 5;
B(17,4) = 3;
B(16, 19) = 2.3;
B(5, 18) = 4.5;
Bhat = inpaint_nans(B);

surf(B,'marker','o')
hold on
surf(Bhat)

Inpainted surface from nearly empty array

Edit:

For those interested in whether inpaint_nans can handle more complex surfaces, I once took a digitized Monet painting (seen on the left hand side, then corrupted it by deleting a random 50% of the pixels. Finally, I applied inpaint_nans to see if I could recover the image reasonably well. The right hand image is the inpainted one. While the resolution is low, the recovered image is a decent recovery.

Garden at Sainte-Adresse

As another example, try this:

[x,y] = meshgrid(0:.01:2);
z = sin(3*(x+y.^2)).*cos(2*x - 5*y);
surf(x,y,z)
view(-23,40)

base trig surface

Now, delete about 7/8 of the elements of this array, replacing them with NaNs.

k = randperm(numel(z));
zcorrupted = z;
zcorrupted(k(1:35000)) = NaN;

Recover using inpainting. The z-axis has a different scaling because there are minor variations above and below +/-1 around the edges, but otherwise, the latter surface is a good approximation.

zhat = inpaint_nans(zcorrupted);
surf(x,y,zhat)
view(-23,40)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文