Problem with size of a 0-by-1 empty matrix.

10 vues (au cours des 30 derniers jours)
sam kamiab
sam kamiab le 1 Août 2019
hi dear friends. For computing the jaccard similarity index of a graph's nodes I need to compute a pairwise intersection among their neighborhood matrix and find the number of common neighbors. for example (2, [1,4], [], 2) are neighbors of nodes 1 to 4 and node 3 is alone. The problem is that when I compute the intersection, the size of results for the alone node is [1x0 double] or [0x1 double] like this:
(Int)
[ 2] [1x0 double] [1x0 double] [ 2]
[1x0 double] [1x2 double] [0x1 double] [1x0 double]
[1x0 double] [0x1 double] [0x1 double] [1x0 double]
[ 2] [1x0 double] [1x0 double] [ 2]
and the size of [0x1 double] vector will be computed as 1 while the correct answer is 0 like this!
the size matrix:
1 0 0 1
0 2 1 0
0 1 1 0
1 0 0 1
I have the same problem with union, how can I fix this problem??
related part of my code is this:
[ii, jj] = ndgrid(1:Size) %Size=number of nodes
H=NeighborsS(L1,:); %%% I have a multiplex network and L1 is the layernumber
result= arrayfun(@(n) intersect(H{ii(n)},H{jj(n)}), 1:Size^2, 'uni', 0);
Int(:,:,L1) = reshape(result,Size,Size);
IntNum=cellfun('size',Inti,2);

Réponse acceptée

Walter Roberson
Walter Roberson le 1 Août 2019
empty = cellfun(@isempty, result);
result(empty) = {zeros(0,0)}; %same size of emptiness for each empty result
Int(:,:,L1) = reshape(result,Size,Size);
IntNum = cellfun('size',Inti,2);
  1 commentaire
sam kamiab
sam kamiab le 1 Août 2019
tnx alot. it works:)

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 1 Août 2019
You don't actually care about the size of the neighbors vector, you care about the number of neighbors. Use @numel instead of 'size' in your cellfun call.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by