Is fairly simple, yes, but have to think about it a while to find "the Matlab way"... :)
There's always another way, but what came to me first...
>> A=[1:13].';
>> B=randi(20,20,3)
B =
2 2 19
11 5 4
16 19 6
19 4 3
3 17 3
12 11 18
10 20 12
1 2 11
7 9 3
4 3 18
16 20 13
7 1 8
11 16 11
4 17 9
13 18 2
6 2 5
14 8 3
14 6 4
15 17 5
10 9 9
>> [n,bin]=histc(B(:),A);
>> C=[A n]
C =
1 2
2 5
3 6
4 5
5 3
6 3
7 2
8 2
9 4
10 2
11 5
12 2
13 2
>>
D takes a little thinking...first reshape bin to match the shape of B so can get the row corresponding to position...
>> bin=reshape(bin,[],3);
>> D=zeros(length(A),length(B));
>> for i=1:length(A)
[r c]=find(bin==A(i))
r=unique(r);
t(i,r)=r;
end
>> D=[n t]
D =
2 0 0 0 0 0 0 0 8 0 0 0 12 0 0 0 0 0 0 0 0
5 1 0 0 0 0 0 0 8 0 0 0 0 0 0 15 16 0 0 0 0
6 0 0 0 4 5 0 0 0 9 10 0 0 0 0 0 0 17 0 0 0
5 0 2 0 4 0 0 0 0 0 10 0 0 0 14 0 0 0 18 0 0
3 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 16 0 0 19 0
3 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 16 0 18 0 0
2 0 0 0 0 0 0 0 0 9 0 0 12 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 0 12 0 0 0 0 17 0 0 0
4 0 0 0 0 0 0 0 0 9 0 0 0 0 14 0 0 0 0 0 20
2 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 20
5 0 2 0 0 0 6 0 8 0 0 0 0 13 0 0 0 0 0 0 0
2 0 0 0 0 0 6 7 0 0 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 11 0 0 0 15 0 0 0 0 0
>>
The duplicated nodes not possible in a real dataset I'd presume; I just added so could use the randomized array w/o having to clean it up here.
Oh, above assumes the row is the same as the node in assigning r as the row, that needs to be the elements of A for the corresponding index if the array of nodes isn't 1:N.
Best Answer