Search code examples
matlabmatlab-figure

using maltab to plot a matrix


I have a matrix. The entries are all integers. For example, my matrix would look like this

M = 1 1 1 2 2 2 2 3 3
    1 1 1 2 2 2 2 3 0
    4 4 4 5 5 5 5 0 0
    4 4 4 5 5 5 0 0 0
    4 4 4 5 5 0 0 0 0
    4 4 4 5 0 0 0 0 0
    6 6 6 0 0 0 0 0 0
    6 6 0 0 0 0 0 0 0
    6 0 0 0 0 0 0 0 0

I wonder if matlab has some neat function to plot the boundary of these sets generated by this matrix?


Solution

  • I think you are looking for a variation of contour plot. Don't forget that it will flip Y axis.

    contour(M)
    g = gca;
    g.YDir = 'reverse';
    
    for jj=1:size(M,1)
        for ii=1:size(M,2)
            text(jj,ii,num2str(M(ii,jj)));
        end
    end
    

    enter image description here