如何在 matlab 中绘制矩阵,将矩阵索引视为 x-y 轴上的坐标

How do I plot matrix in matlab, considering matrix indexes as co-ordinates on a x-y axis

在我下面的代码中,region 是一个 1000x1500 的矩阵。我想在 X-Y 图表纸上绘制此矩阵的值。因此,我假设的图表纸由来自 1:1000 的 X 值和来自 1:1500.

的 Y 值组成
function plotRegion(region)
    figure;
    [a,b]=size(region);
    hold on;

    for i=1:a
        for j=1:b
            if(region(i,j)>0)
                plot(i,j ,'.' );
            end
        end
    end
    hold off;
end

我正在遍历矩阵的每个值,每当我看到值大于 0 时,我就在我的图中放一个点。尽管上面的代码有效,但在我的计算机上 运行 大约需要 70 秒。

我想我遗漏了一些非常基本的东西,这可以非常有效地完成,我只是想不到。请帮助我重写这段代码以满足我的目的。

我的情节代码的示例输出:

您可以使用 find 代替迭代 region,并使用 scatter 代替 plot

如果你不关心点的颜色,你可以简单地做:

[Y, X] = find(region > 0);
plot(X, Y, '.')

如果您想保留颜色:
还是花了太长时间...

[Y, X] = find(region > 0);

for i = 1:length(X)
    plot(X(i), Y(i), '.' );
end

考虑使用 scatter 而不是 plot
scatter更适合绘制点:

[Y, X] = find(region > 0);    
C = 1:length(X); %Colors
C = mod(C, 7);   %Try to fix the colors
scatter(X, Y, [], C, '.');