"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 cochange with it (all the friends), then go on and find all that cochange 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
