Sum if multiple conditions satisfied across different arrays
이전 댓글 표시
Hello everyone!
Would love some help to create an optimal solution to this problem.
Considering that I have the adjacency matrix describing a network:
adj = [0 1 1 1
1 0 0 1
1 0 0 1
1 1 1 0];
and a binary matrix:
array = randi([0 1],4,10)
where the columns are each node in the network, and the rows are the state of the network at each time step. To clarify, if node 1 is '1' at time step 2, it would be represented as:
array(2,1) = 1
How would I create a vector counting the number of times between time steps that these two conditions are satisfied:
- 1. Node i is: 0 at time t and 1 at time t+1
- 2. Node i is: not neighbors with any nodes that have the value of 1 at time t
So far, through the wonderful help of the MATLAB community, the list of neighbors for each node may be extracted as follows:
adj= [0 1 1 1
1 0 0 1
1 0 0 1
1 1 1 0];
[i,j]=find(adj);
neighbour_list = accumarray(i,j,[size(adj,1), 1],@(x) {sort(x).'});
And the first condition may be found as follows:
condition1_satisfied_count = sum( diff(array,1,1)==-1 ,2)
I just have no clue how to test for both of these conditions and count them. The solution needs to be inherently fast and efficient since this code will be run >100,000 times.
Really appreciate it :)
Cheers, Abhishek
댓글 수: 4
Jos (10584)
2018년 2월 14일
Just to show you a faster way to get the neighbours:
nb_list = arrayfun(@(r) find(adj(r,:)), 1:size(adj,1),'un',0) ;
% nb_list{k} holds the neighbours of node k
Abhishek Varghese
2018년 2월 14일
Jos (10584)
2018년 2월 14일
Perhaps, but it might also depend on the number of nodes and the number of neighbours. cond2 then becomes:
cond2 = @(i,t) all(S(nb_list{i},t)==0) ;
% true when the neighbours of i are not active at time t
A first checks shows the same results :D You should check timings though.
Abhishek Varghese
2018년 2월 14일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Sparse Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!