how to write such a loop?

2 views (last 30 days)
Shuoguo
Shuoguo on 12 Apr 2011
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
  4 Comments
Shuoguo
Shuoguo on 12 Apr 2011
oh i skipped something...lets say the so called vector is, for this case, 8 random numbers like V=[1 2 3 4 8 7 6 5].each element of V, Vi has a spin variable either 0 or 1 (only two "states", 0 or 1 for simplicity. It can be 10 states numbered from 1~10).
the interaction between any two elements forms a 8*8 matrix P, if Vi and Vj have the same spin, then P_ij=1, else 0. so by looking at P, an 1 simply means there is an interaction between two elements and 0 means no interaction.
if Vi interacts with Vj, and Vj interacts with Vk, then Vi interacts with Vk, and so on.
so my question is if i begin with any Vi, i want to find all that can interact with it directly (like Vi-Vj) or indirectly (Vi-Vk).
thanks
Shuoguo
Shuoguo on 12 Apr 2011
by the way the so called "spin" variable will be a separate vector that defines some property of Vi...i gave random numbers to vector V since the actual value is not critical when we are talking about spin interactions.

Sign in to comment.

Answers (3)

the cyclist
the cyclist on 12 Apr 2011
Here is a solid kick in the right direction. The while-loop only tests if you have found all possible friends (based on the size of the original array). It does not test whether you have reached a "steady-state" because the network does not extend to all possible people. Should be easy to add that, if you want.
A = [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];
N = size(A,1); % Assume symmetric, square array A
allFriends = 1:N;
oldFriends = 1
pause(1)
while not(numel(oldFriends)==numel(allFriends))
for nf = oldFriends
newFriends = find(A(nf,:));
oldFriends = union(oldFriends,newFriends); % Automatically sorted by MATLAB, which is important for while test.
end
oldFriends
pause(1)
end
  1 Comment
Shuoguo
Shuoguo on 12 Apr 2011
when i run this i think matlab stuck somewhere?

Sign in to comment.


Shuoguo
Shuoguo on 12 Apr 2011
thanks the cyclist...i also just worked out a code (bigger than yours)and so far i am not eyeballing any bugs.
P =
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
while ~isempty(P>0)
[rs,~]=find(P>0);
if isempty(rs)
return;
else
rs=rs(1);
end
func = @(x) find(P(x,:)>0);
c = rs;
oldc = c;
while true
c=oldc;
[r c] = func(c);
c=sort(unique(c))';
if isequal(c,oldc)
break;
end
oldc=[oldc c];
oldc=sort(unique(oldc));
end
P(c,:)=0;
end
  2 Comments
Matt Fig
Matt Fig on 12 Apr 2011
With that P, your code simply returns....
Shuoguo
Shuoguo on 12 Apr 2011
get a wrong matrix...fixed

Sign in to comment.


Walter Roberson
Walter Roberson on 12 Apr 2011
I didn't catch the bit about the spin variable, but the other part sound like standard matrix traversal that is implemented by taking successive matrix powers.

Categories

Find more on Discrete Math in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!