COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### indexing the input matrix and plotting through for loop

• Email
• 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

See related articles to this posting

```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 (6403) 3/10/2011 8:13:05 PM

1 Replies
20 Views

Similar Articles

12/12/2013 10:54:21 PM
[PageSpeed]

Similar Artilces:

Is there any non loop implement to find the index of a vector in the matrix?
Is there any non loop implement to find the index of a vector in the matrix? for example: a=[1 2] b=[4 5;6 7;8 7;1 2;2 3] I want to return 4 (which is the index of a in b) Thank you ! example 2: a=[1 2;5 4] b=[4 5;6 7;8 7;1 2;2 3] I want to return [4;1] (I think [4 5] is the same as [5 4]) How about this: a=[1 2]; b=[4 5;6 7;8 7;1 2;2 3]; %return the row index where theres the number you are searching for firstcolumnrow=find(b(:,1)==a(1,1)) secondcolumnrow=find(b(:,2)==a(1,2)) if firstcolumnrow and seconcolumnrow are equal this is the row that you are searching for "zedong "...

3-index Matrix multiply without loop
Hello, Any suggestions how to vectorize the loop shown below (except k varies from 1:(large_number) )? Thanks much, Eric % my intent FOR EXAMPLE M1 = [1 2 3 4 5 6]; M2 = [1 0 -1 0 1 0]; x1 = [1 0]; x2 = [1 1]; A1 = x1*M1 A2 = x2*M2 %with loop and 3-index array X=[x1;x2]; M(:,:,1)=M1; M(:,:,2)=M2; for k = 1:2 % A(k,:)=X(k,1:2)*M(1:2,1:3,k); end A(1,:) % matches A1 A(2,:) % matches A2 Eric Carlson schrieb: > Hello, > Any suggestions how to vectorize the loop shown below (except k varies > from 1:(large_number) )? > > Thanks much, > Eric > > ...

[plot] Updating matrix data in plot
Hello everyone! I have a little problem. I plot a matrix data and then I want to update 'xdata' and 'ydata' by using 'set'. This code explains my problem: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% x = [1 1 2 2 1; 5 5 6 6 5]; y = [1 2 2 1 1; 5 6 6 5 5]; h = plot(x',y'); %I plot some (i this example two) rectangles. axis([0 7 0 7]); x = x+1; %I add a constant to make an illusion of movement. y = y+1; %As above. set(h,'xdata',x','ydata',y') % <- error %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% When I use 'set'...

Plotting a matrix
Suppose we have a matrix A with zeros and ones, maybe A = floor(rand(N)+0.5). How can I draw or plot this so that each 1 become a dot and each 0 become nothing? I want to view the matrix graphically (it represents a cellular automata). / Thanks for any help! Hi John, try >> spy(A) Titus "john" <nomail@noserver.com> wrote in message news:40867585\$1@puffinus.its.uu.se... > Suppose we have a matrix A with zeros and ones, maybe A = > floor(rand(N)+0.5). > > How can I draw or plot this so that each 1 become a dot and each 0 become > nothing? I want ...

Matrix index
Hi, If I have a matrix A with dimension 5 x 5 and I want create a submatrix: B = [A(1,2), A(2,3); A(4,3), A(5,1)]; How can I create this submatrix with the corresponding x-indeces and y-indeces, xc and yc? xc = [1,2;4,5]; yc = [2,3;3,1]; I tried A(xc, yc) but it failed to produce B. What is the correct approach? Thanks a lot "Frank " <allinone_2003@yahoo.com.hk> wrote in message <i0f6bi\$s72\$1@fred.mathworks.com>... > Hi, > > If I have a matrix A with dimension 5 x 5 and I want create a submatrix: > > B = [A(1,2), A(2,3); A(4,3...

how to index a matrix
Hi, I have a sequence of square matrix. I want them to be indexed, e.g A1, A2, A3,... Is there any command to do that? Any help will be greatly appreciated ywzgz@yahoo.com (John) writes: >Hi, >I have a sequence of square matrix. I want them to be indexed, e.g >A1, A2, A3,... >Is there any command to do that? Maybe cell arrays are best, but you can do what you want using sprintf and eval - several examples are online. "John" <ywzgz@yahoo.com> wrote in message news:fe6qcld2q9zq@legacy... > Hi, > I have a sequence of square matrix. I want them to be index...

Input for livermore loops
Is it possible to use some random input data for using the kernels given in livermore loops ? Is there any limit on the maximum size of the array/data used as input to the kernel ? For example, in kernel 12 (given below) is there any restriction on the size of the value 'n' being used in this loop? /* ******************************************************************* * Kernel 12 -- first difference ******************************************************************* */ ntest = 12; do { for ( k=0 ; k<n ; k++ ) { x[k] = y[k+1] - y[k]; ...

matrix indexing 416858
Is there a way to index matrix A such that it returns a column vector equal to: 1 2 3 4 5 6 7 8 9 without first transposing matrix A? That is, can I save a step by avoiding the transpose? ---------------------------------- >> A=[(1:3); (4:6); (7:9)] A = 1 2 3 4 5 6 7 8 9 >> A(:) ans = 1 4 7 2 5 8 3 6 9 On 28/12/10 12:48 PM, gkk gkk wrote: > Is there a way to index matrix A such that it returns a column vector > equal to: > > 1 > 2 > 3 > 4 ...

plots loops and colours
Hi, I'm trying to repeat a number of times a plot but with different data. Can anyone help me out? I have a matrix X (3264,432) that is subdivided in days (192,432). I also have a Repeat array that tells when I change from one day to the other i.e. [1 192 384 576 768 ...] I want to plot the same graph for everyday but changing colour every repetition. So far I've got for m=1: (length(Repeat) - 1) plot(X((Repeat(m):Repeat(m+1)),73),X((Repeat(m):Repeat(m+1)),74)) axis equal hold on end But all my plots have the same colour. Any idea how I can have each plot in a diffe...

For loop output into matrix
Hi there, I have the following problem how can I get the output of my for loop into a matrix. For some reasons the for loop only considers real and integer values. for a=0.3:-0.15:-0.3; for b=0.3:-0.15:-0.3; vf1(a,b) = sqrt(3/(-(5/3).*a+b+2.*sqrt((a/3).*((4/3).*a-b)))) end end Thanks in advance you for your help! Seb "Sebastian " <sebastian.meng@meng-muenchen.de> wrote in message <il0kt4\$sb0\$1@fred.mathworks.com>... > Hi there, > > I have the following problem > > how can I get the output of my for loop into a matrix....

Matrix Indexing 387875
Suppose I've got a 6-D (e.g. 3 x 5 x 2 x 2 x 2 x 6) matrix--say data = repmat(1:10, 72, 1); data = reshape(data, [3, 5, 2, 2, 2, 6]); Could someone tell me why data(:, :, [2, 1, 1, 1]) returns a 3D (3 x 5 x 4) matrix while data(:, :, 2, 1, 1, 1) returns what I want, namely a 2D (3 x 5) matrix? "james bejon" <jamesbejon@yahoo.co.uk> wrote in message <hoihf4\$8uk\$1@fred.mathworks.com>... > Suppose I've got a 6-D (e.g. 3 x 5 x 2 x 2 x 2 x 6) matrix--say > > data = repmat(1:10, 72, 1); > data = reshape(data, [3, 5, 2, 2, 2, 6]); &...

How to Index a square matrix
Hello! A is a square matrix of zeros with an even number of columns and thus rows. I want to index the row-elements of A in the following repetitive fashion and turn them into ones: row1: Every second element, starting with the first column row2: leave unchanged row3: Every second element, starting with the second column row4: leave unchanged row5: see row1 row6: see row2 row7: see row3 For all rows of A How can I best accomplish this? Many Thanks Forben gunther wrote: > > Hello! > > A is a square matrix of zeros with an even number ...

3D matrix indexing
Hi, this is probably very simple but I can't figure it out. I have a 3D matrix, from which I want to select several vectors in the following way: if A is the matrix, I want to select list a vectors A(r,c,:), where r and c are are lists of numbers for the first and second subscript of A. To give an example for A = 3x3x4: r = [3, 2, 3] c = [2, 1, 3] The result should be a matrix of the form A(3,2,1) A(3,2,2) A(3,2,3) A(3,2,4) A(2,1,1) A(2,1,2) A(2,1,3) A(2,1,4) A(3,3,1) A(3,3,2) A(3,3,3) A(3,3,4) FYI this is for a Markov process model in which A is the transition matrix for 2 reacting spe...

how to calculate matrix with out for loop
I = [ 1 1 1; 0 1 1; 0 0 0]; [m n] = size(I); h = zeros(9, 1); for i=1:m for j=1:n Z=I(i,j); h(Z+1)=h(Z+1)+1; end end above same code but should i use without for loop or use one for loop please tell me suitable answer anybody On Oct 13, 2:55=A0pm, "Jamal Mohamed" <in.ja...@yahoo.com> wrote: > I =3D [ 1 1 1; 0 1 1; 0 0 0]; > [m n] =3D size(I); > h =3D zeros(9, 1); > > for i=3D1:m > =A0 =A0 for j=3D1:n > =A0 =A0 =A0 =A0 Z=3DI(i,j); > =A0 =A0 =A0 =A0 h(Z+1)=3Dh(Z+1)+1; > =A0 =A0 end > end > > above same code but s...

for loop to sum matrix
Hi, I'm trying to create a function that sums a matrix using a for loop. It's supposed to give the exact same output as the sum function would. Here's all I got so far: function answer=addMatrixWithFor(n) a=size(n) i=1 j=1 for(i<a(1,1)) for (j<a(1,2)) answer= I'm not really sure where to go from here though. I know that I need to create a matrix so that the sum of each column is put in that matrix. I need to incrementthe row by +1 every time and add the data in that column to the previous answer. However, I'm having trouble actually putting this in...

Creating Matrix from Loop
I've been using the CN method to solve a 1D heat equation and am having trouble creating a matrix of the results produced from the loop. Could someoneone point me in the right direction please? I would like to create a matrix of the results created from Un Here is my code: h=0.1 k=0.005 Un=0:h:1; Un=30*cos(Un); n=length(Un); n1=n-1; Un1=Un; b=10.526*ones(1,n); c=-4.263*ones(1,n); a=c;c(1)=2*c(1);a(n)=2*a(n); r=ones(1,n); t=0; for F=1:10 t=t+k; r(1)=-6.526*Un(1)+8.526*Un(2); for i=2:n1; r(i)=4.263*Un(i-1)-6.526*Un(i)+4.263*Un(i+1); end r(n)=30*exp(-(2.92^2)*t)*cos(1); bet...

Plotting Covariance Matrix?
How can I nicely plot my cov matrix like http://wwwrses.anu.edu.au/~malcolm/na/naplot_figs/cor_sm.gif or maybe even better? Then only method I can think of is to plot each dot and decide the color by the value which might be to time-demanding. ysongpsu@gmail.com wrote: > How can I nicely plot my cov matrix like http://wwwrses.anu.edu.au/~malcolm/na/naplot_figs/cor_sm.gif > or maybe even better? Then only method I can think of is to plot each > dot and decide the color by the value which might be to time-demanding. The method dscribed in this Statistics Toolbox demo <http://www....