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

2 次查看(过去 30 天)
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);

采纳的回答

Walter Roberson
Walter Roberson 2019-8-1
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 个)

Steven Lord
Steven Lord 2019-8-1
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.

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by