Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为某些点绘制不同颜色的网格(MATLAB)_Matlab_Plot_Grid - Fatal编程技术网

如何为某些点绘制不同颜色的网格(MATLAB)

如何为某些点绘制不同颜色的网格(MATLAB),matlab,plot,grid,Matlab,Plot,Grid,问题 我有一个尺寸为NxN的正方形网格,网格点/节点之间的间距恒定。我想绘制这个网格,但有些点是特殊的,希望它们以不同的颜色绘制 期望值 预期的绘图应该是这样的,但某些块必须具有不同的颜色 代码 下面给出了我荒谬而缓慢的解决方案: N = 80; x = 1:1:N; y = 1:1:N; rx = randi([1 80],1,1000); %represents the x coordinates of the special points ry = randi([1 80],1,10

问题

我有一个尺寸为NxN的正方形网格,网格点/节点之间的间距恒定。我想绘制这个网格,但有些点是特殊的,希望它们以不同的颜色绘制

期望值

预期的绘图应该是这样的,但某些块必须具有不同的颜色

代码

下面给出了我荒谬而缓慢的解决方案:

N = 80;
x = 1:1:N;
y = 1:1:N;

rx = randi([1 80],1,1000); %represents the x coordinates of the special points 
ry = randi([1 80],1,1000); %represents the y coordinates of the special points 

[X,Y] = meshgrid(x,y);
Z = zeros(80,80);
figure(1)
surf(X,Y,Z);

%Abandon surf, use scatter instead
figure(2)
for i=1:N
    for j=1:N        
        plot(x(i),y(j),'bo');
        hold on
    end
end

for i=1:1000
    plot(rx(i),ry(i),'ro');
    hold on    
end
grid on    


因为我的眼睛在流血,正确的方法是什么?非常感谢。

颜色由Z控制。因此,将Z设为不同的值

linear_indx=sub2ind([N,N],rx,ry);
Z(linear_indx)=1;
更改颜色贴图(或制作自己的)以获得不同的颜色集。或者使用
surf(X,Y,Z,C)


上帝。当然它是由Z控制的。谢谢@安杰洛斯不用担心。公平地说,我认为对于这个例子来说,
imagesc
imshow
将是更好的选择,但我认为您的实际代码可能要复杂一些