matlab中的L1范数等高线图

L1 norm contour plot in matlab

我正在尝试在 matlab 中绘制 L1 范数的水平集。但它根本不起作用,我被困住了。有帮助吗?

x = linspace(-1,1,10);
y = linspace(-1,1,10);
[xm,ym] = meshgrid(x,y);
z = sum(abs(xm-ym));
surfc(x,y,z)

根据definition of L1 norm:

把你的第四行改成这样
z = abs(xm)+abs(ym);

使用 with bsxfun 可以更有效地避免生成矩阵 xmym:

x = linspace(-1,1,10);
y = linspace(-1,1,10);
z = bsxfun(@plus, abs(x), abs(y).');
surfc(x,y,z)

两种方法中的任何一种都会产生:

为了获得更好的图片,您应该增加采样,并可能移除表面边缘:

x = linspace(-1,1,100);
y = linspace(-1,1,100);
z = bsxfun(@plus, abs(x), abs(y).');
surfc(x,y,z,'edgecolor','none')