indexing the input matrix and plotting through for loop

  • Follow


Hi every body,

I have a set of data which I want to plot them. I need to extract the data of the second column and the third  column  correspond to the equal element in column one.

simply explaining with an example assume that my data is stored in matrix A which iis:
  A=[1     5     1;...
        1     8     2;...
        2     3     1;...
        2     10   3;...
        1     7     4;...
        3     1     5];

a=A(:,1);
b=A(:,2);
c=A(:,3);
the first index should be:
index1=[1 ; 1; 0; 0; 1; 0]
then:
m=b(index1);>>>>>>>[5 8 7]
t=c(index1);>>>>>>>>[1 2 4]
plot (t,m, '-');

the second index will be:
index2=[0; 0; 1; 1; 0; 0];
then:
m=b(index2);>>>>>>>[3 10]
t=c(index2);>>>>>>>>[1 3]
plot (t,m, '-');

index3=[0; 0; 0; 0; 0; 1];
then:
m=b(index3);>>>>>>>[5]
t=c(index3);>>>>>>>>[1 5]
plot (t,m, '-');

I created a following loop, but It overdo the things, this is because it iterates through the loop and each time compare the value even though the value might be already checked before. can you tell me how can I fix it?

a=A(:,1);
b=A(:,2);
t=A(:,3);
% here is to define different collor for plots from defects
c = ['r';'g';'b';'y';'m';'c'];
l = length(c);

figure
for i=1:size(a,1)
    index= a(i)==a;
    m=b(index);
    t=t(index);
    col = [c(mod(i-1,l)+1), '-'];
    plot(t,m,col);
    hold on
    grid on
end

thanks so much,
s.
0
Reply khanmoradi (52) 3/10/2011 7:43:06 PM

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

a=A(:,1);
b=A(:,2);
t=A(:,3);
m = size(A,1);
[Val I J] = unique(a);
i = accumarray(J(:), (1:m)', [], @(x) {sort(x)});

c = ['r';'g';'b';'y';'m';'c'];
l = length(c);

figure
hold on
grid on
for k=1:length(i)
    index=i{k};
    col = [c(mod(k-1,l)+1), '-'];
    plot(b(index),t(index),col);   
end

% Bruno
0
Reply b.luong5955 (6341) 3/10/2011 8:13:05 PM


1 Replies
14 Views

(page loaded in 0.16 seconds)

Similiar Articles:













7/29/2012 5:56:44 AM


Reply: