如何使用Matlab在图表中显示矩阵及其索引号的值

时间:2014-08-27 15:42:24

标签: matlab matlab-figure

我有一个Matrix例如

A = [1 2 3; 3 4 5; 7 8 9]

我希望显示值与其位置索引相关,以便人们可以看到值为1的A(1,1)。其他人相似。

我想在x轴显示值为a11,a12,a13 ....在Y轴显示相应的值1,2,3

请建议。

1 个答案:

答案 0 :(得分:1)

你可以用这个:

[ii, jj] = meshgrid(1:size(A,1), 1:size(A,2));
labels = strcat('(', num2str(ii(:)), ',' ,num2str(jj(:)), ')');
stem(reshape(A.',[],1)); %'// or plot, or bar, or...
set(gca, 'xtick', 1:numel(A))
set(gca, 'xticklabel', labels)
xlim([0, numel(A)+1])

enter image description here


要更改每个点的颜色:您可以使用hold all

[ii, jj] = meshgrid(1:size(A,1), 1:size(A,2));
labels = strcat('(', num2str(ii(:)), ',' ,num2str(jj(:)), ')');
hold all
B = A.';
for n = 1:numel(ii)
    stem(n,B(n)); %'// or plot, or bar, or...
end
set(gca, 'xtick', 1:numel(A))
set(gca, 'xticklabel', labels)
xlim([0, numel(A)+1])

enter image description here

或者您可以手动定义一组颜色并在循环中连续使用它们:

[ii, jj] = meshgrid(1:size(A,1), 1:size(A,2));
labels = strcat('(', num2str(ii(:)), ',' ,num2str(jj(:)), ')');
colors = hsv(numel(A)); %// define colors
B = A.';
hold on
for n = 1:numel(ii)
    stem(n,B(n), 'color', colors(n,:)); %'// or plot, or bar, or...
end
set(gca, 'xtick', 1:numel(A))
set(gca, 'xticklabel', labels)
xlim([0, numel(A)+1])

enter image description here