Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Finding number starting each row

Subject: Finding number starting each row

From: Diego Zegarra

Date: 18 Feb, 2009 17:36:01

Message: 1 of 4

Lets say I have the following matrix each number representing a job, (each number can only be present once),

A = [ 7 3 2 6;8 5 1 12;10 4 11 9];

I would like to create a matrix of 12 x 12 in this case since there are 12 jobs and put a 1 in the (1,1) index is 1 is a job that starts a row. In this example the 12 x 12 matrix would have a 1 only at (7,7) , (8,8) and (10,10).

I know this can be done with a loop but I am sure there is a faster way,

j = 0
for i =1:12
    j = j + 1
   E= find(A(:,1)== i);
   if E >= 1
      E(i,i) = 1;
   end
end

Hope I made myself clear, if not let me know!

Thanks!

Subject: Finding number starting each row

From: Donn Shull

Date: 18 Feb, 2009 19:32:02

Message: 2 of 4

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gnhgu1$n8p$1@fred.mathworks.com>...
> Lets say I have the following matrix each number representing a job, (each number can only be present once),
>
> A = [ 7 3 2 6;8 5 1 12;10 4 11 9];
>
> I would like to create a matrix of 12 x 12 in this case since there are 12 jobs and put a 1 in the (1,1) index is 1 is a job that starts a row. In this example the 12 x 12 matrix would have a 1 only at (7,7) , (8,8) and (10,10).
>
> I know this can be done with a loop but I am sure there is a faster way,
>
> j = 0
> for i =1:12
> j = j + 1
> E= find(A(:,1)== i);
> if E >= 1
> E(i,i) = 1;
> end
> end
>
> Hope I made myself clear, if not let me know!
>
> Thanks!

Hi Diego,

There are probably a lot of ways to do this. One with a shorter loop would be:

E = zeros(12,12);
jobs = A(:,1);
for index = 1:numel(jobs)
    E(jobs(index), jobs(index) =1;
end

Hope this helps,

Donn

Subject: Finding number starting each row

From: us

Date: 18 Feb, 2009 19:34:01

Message: 3 of 4

"Diego Zegarra"
> A = [ 7 3 2 6;8 5 1 12;10 4 11 9];
> I would like to create a matrix of 12 x 12 in this case since there are 12 jobs and put a 1 in the (1,1) index is 1 is a job that starts a row. In this example the 12 x 12 matrix would have a 1 only at (7,7) , (8,8) and (10,10)...

two of the many solutions

     m=[
           7 3 2 6
           8 5 1 12
          10 4 11 9
     ];
     rs=sparse(m(:,1),m(:,1),1,12,12);
     ra=accumarray([m(:,1),m(:,1)],1,[12,12]);
     isequal(ra,full(rs))
% ans = 1

us

Subject: Finding number starting each row

From: Jos

Date: 18 Feb, 2009 19:59:02

Message: 4 of 4

"us " <us@neurol.unizh.ch> wrote in message <gnhnr9$aif$1@fred.mathworks.com>...
> "Diego Zegarra"
> > A = [ 7 3 2 6;8 5 1 12;10 4 11 9];
> > I would like to create a matrix of 12 x 12 in this case since there are 12 jobs and put a 1 in the (1,1) index is 1 is a job that starts a row. In this example the 12 x 12 matrix would have a 1 only at (7,7) , (8,8) and (10,10)...
>
> two of the many solutions
>
> m=[
> 7 3 2 6
> 8 5 1 12
> 10 4 11 9
> ];
> rs=sparse(m(:,1),m(:,1),1,12,12);
> ra=accumarray([m(:,1),m(:,1)],1,[12,12]);
> isequal(ra,full(rs))
> % ans = 1
>
> us

Here is another solution for this quite strange assignment ...

m=[ 7 3 2 6 ; 8 5 1 12 ; 10 4 11 9]
X(12) = 0 ;
X(m(:,1)) = 1 ;
X = diag(X)

Jos

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us