MATLAB: How to plot the different values in a matrix as different entities in a single plot

matrixmoving plotplot

Here's my matrix:
1 3 4 4 1
3 1 2 4 3
4 2 1 2 2
2 4 4 4 1
2 2 3 4 4
I want to plot 1 as a red circle, 2 as a blue square and so on. Also, the values in my matrix keep changing after every iteration. I would like my plot to also change accordingly. Any help would be appreciated. Thanks!

Best Answer

This plots the row- and column-subscripts, each with the appropriate marker and color.
The Code
M = [1 3 4 4 1
3 1 2 4 3
4 2 1 2 2
2 4 4 4 1
2 2 3 4 4];
for k1 = 1:max(M(:))
I{k1} = find(M == k1); % Linear Indices For Each Value
end
mc = {'or', 'sb', 'gp', 'c^'}; % Colors & Markers
figure
hold all
for k1 = 1:max(M(:))
[r,c] = ind2sub(size(M), I{k1}); % Convert Linear Indices To Subscripts
plot(c, r, mc{k1}) % Plot Appropriate Markers At Subscripts
end
axis([0 size(M,2)+1 0 size(M,1)+1]) % Set Axis Limits
set(gca, 'XTick',1:size(M,2), 'YTick',1:size(M,1)) % Define Tick Values
set(gca, 'YDir','reverse')
xlabel('Column Index')
ylabel('Row Index')
legend('1', '2', '3', '4', 'Location','EastOutside')
As ‘M’ changes, you will have to re-run it, probably in a loop or as a function called in a loop, with ‘M’ as the argument.
The Plot
EDIT Axis limits and tick values now change with thge size of ‘M’, added the legend.