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:
how to write such a loop?

Subject: how to write such a loop?

From: Shuoguo

Date: 12 Apr, 2011 16:59:07

Message: 1 of 2

hello,

if i have a n*1 vector. every element can interact with each other.an 1 at position (i,j) means member i will change its state with member j.an zero means they do not change together. so all the interactions between any two elements can be expressed in a n*n matrix (diagnal is 0 means i^th element do not interact with itself):

     0 1 0 1 0 0 0 0
     1 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0
     1 0 0 0 1 0 0 0
     0 0 0 1 0 0 0 0
     0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0

now my question is: i want to begin with a random place, and find all that co-change with it (all the friends), then go on and find all that co-change with its friends (new friends), and go on until no new friends can be find.

eg.

1. i begin with element 1.

2. i go to first row, find element 2 and 4 are friends

3. then i go to row2 find 1 (not new friends); go to row 4 find 1 (again) and 5 (new friends);

4. then continue to row 5, find 4 (not new friends)
5. mark 1,2,4,5 as friends.
6. go back to step 1, find a new element (other than 1,2,4,5) to start with.

i have some bigger matrix to work with (100*100)...and this has been puzzled me for a while, please help me out.

thanks a lot.

Shuoguo
stop here since no new friends were found.

Subject: how to write such a loop?

From: Roger Stafford

Date: 16 Apr, 2011 20:52:04

Message: 2 of 2

"Shuoguo " <shuoguo@gmail.com> wrote in message <io20cr$s7h$1@fred.mathworks.com>...
> hello,
>
> if i have a n*1 vector. every element can interact with each other.an 1 at position (i,j) means member i will change its state with member j.an zero means they do not change together. so all the interactions between any two elements can be expressed in a n*n matrix (diagnal is 0 means i^th element do not interact with itself):
>
> 0 1 0 1 0 0 0 0
> 1 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0
> 1 0 0 0 1 0 0 0
> 0 0 0 1 0 0 0 0
> 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0
>
> now my question is: i want to begin with a random place, and find all that co-change with it (all the friends), then go on and find all that co-change with its friends (new friends), and go on until no new friends can be find.
>
> eg.
>
> 1. i begin with element 1.
>
> 2. i go to first row, find element 2 and 4 are friends
>
> 3. then i go to row2 find 1 (not new friends); go to row 4 find 1 (again) and 5 (new friends);
>
> 4. then continue to row 5, find 4 (not new friends)
> 5. mark 1,2,4,5 as friends.
> 6. go back to step 1, find a new element (other than 1,2,4,5) to start with.
>
> i have some bigger matrix to work with (100*100)...and this has been puzzled me for a while, please help me out.
>
> thanks a lot.
>
> Shuoguo
> stop here since no new friends were found.
- - - - - - - - -
  You can make use of the 'any' matlab function in this problem. If x is your interaction matrix and s the starting index, do this:

 p = (1:size(x,1)).'==s;
 q = ~p;
 while any(p~=q)
  q = p;
  p = any(x(q,:)~=0,1).' | q;
 end
 f = find(p.');

The column vector f will contain the indices of all "friends" of s.

Roger Stafford

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