Tallying coincidences of numbers (Part 2) - MATLAB -
following on from: tallying co-incidences of numbers in columns of matrix - matlab
i wondering how best go same task when info processed differently. starting matrix of:
a = 201 202 203 204 201 203 201 203 205 201
i need matrix tallies coincidence of each number column 1 column 2 i.e.
201 202
201 occurs 202 1 time , on (it ignores 202 occurs 201, removing duplicates). stored in 2d matrix min(a):max(a) running downwards both sides.
the desired output above illustration (a) be:
201 202 203 204 205 201 0 1 2 0 0 202 0 0 0 0 0 203 0 0 0 1 0 204 0 0 0 0 0 205 1 0 0 0 0
see if works -
a1 = a-min(a(:))+1 %// offsetted values aout = zeros(max(a1(:))) %// performance: aout(max(a1(:)),max(a1(:)))=0; %// performance trick work, create sure aout isnt defined before. %// source - http://undocumentedmatlab.com/blog/preallocation-performance idx = sub2ind(size(aout),a1(:,1),a1(:,2)) %// performance: idx = (a1(:,2)-1)*size(aout,1) + a1(:,1) unqidx = unique(idx) %// unique indices aout(unqidx) = histc(idx,unqidx) %// set counts of indices places
sample run added row of input info -
>> a = 201 202 203 204 201 203 201 203 205 201 202 201 >> aout aout = 0 1 2 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0
matlab
No comments:
Post a Comment