0

I have a Matrix for example

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

I want to show the values with repect to its position Index so that one can see A(1,1) with value 1. similary for others.

I want to show values as a11, a12, a13....at the x axis and corresponding values 1, 2, 3 at the Y axis

Kindly suggest.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
bsnayak
  • 58
  • 7
  • I'm not really clear on what you're asking. Do you have an example of what you're trying to do? Are you looking to annotate a plot (i.e. write '1' at (1,1), '2' at (1,2), etc.)? – sco1 Aug 27 '14 at 15:45
  • I want to show values as a11, a12, a13....at the x axis and corresponding values 1, 2, 3 at the Y axis Thanks – bsnayak Aug 27 '14 at 15:49
  • @natan I've reopened the question based on the OP's comment: "show values as a11, a12, a13.... __at the x axis__". – Luis Mendo Aug 27 '14 at 21:15
  • @natan I've also added OP's comment in the question to clarify. I hope you don't mind I have undone your closing :-) – Luis Mendo Aug 27 '14 at 21:22
  • @Luis not at all :) , I didn't understand the OP's intention from readin the question before. – bla Aug 27 '14 at 21:25

1 Answers1

1

You could use this:

[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


To change color for each point: you can make use of 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

Or you could define a set of colors manually and use them consecutively within the loop:

[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

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • Hi Luis. Thank you very much. It solved my issue. can you suggest if i want to change color for each alternating point, how can i do that? is there any function available in matlab? – bsnayak Aug 27 '14 at 17:00
  • 1
    @bsnayak - If this answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. – Dev-iL Sep 02 '14 at 09:27