-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathsplit_clusters_by_polarity.m
84 lines (70 loc) · 2.48 KB
/
split_clusters_by_polarity.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function [grp]=split_clusters_by_polarity(grp,pol)
%SPLIT_CLUSTERS_BY_POLARITY Split clusters by relative polarity
%
% Usage: grp=split_clusters_by_polarity(grp,pol)
%
% Description:
% GRP=SPLIT_CLUSTERS_BY_POLARITY(GRP,POL) splits clusters in struct GRP
% (generated by USERCLUSTER) further by record polarities given in POL.
% This is useful for forcing clusters to be divided by focal sphere
% (that way measurements between members of a cluster do not cross
% nodal planes).
%
% Notes:
% - GRP.T & POL must have the same number of elements.
% - GRP.good is reset based on GRP.popcut.
% - GRP.color is amended for the new clusters.
%
% Examples:
% % Does almost nothing (resets GRP.good by GRP.popcut):
% grp=split_clusters_by_polarity(grp,1);
%
% See also: USERCLUSTER, ADJUSTCLUSTERS
% Version History:
% Dec. 12, 2010 - initial version
% Dec. 17, 2010 - update .color too (split clusters retain original
% coloring)
% Jan. 12, 2011 - fix indexing bug, 1/2 color brightness on split
% Jan. 16, 2011 - fix urows indexing bug
%
% Written by Garrett Euler (ggeuler at wustl dot edu)
% Last Updated Jan. 16, 2011 at 13:35 GMT
% todo:
% check nargin
error(nargchk(2,2,nargin));
% check grp
if(~isstruct(grp) || any(~ismember({'T' 'color' 'good' 'popcut'},...
fieldnames(grp))) || ~isequal([max(grp.T) 3],size(grp.color)))
error('seizmo:split_clusters_by_polarity:badInput',...
'GRP must be a struct with fields T, color, good & popcut!');
end
% number of records
nrecs=numel(grp.T);
% check polarity vector
if(~isreal(pol) || ~any(numel(pol)==[1 nrecs]) || any(abs(pol)~=1))
error('seizmo:adjustclusters:badInput',...
'POL must be a vector of 1s & -1s!');
end
% fix polarity vector
if(isscalar(pol)); pol(1:nrecs,1)=pol; end
pol=pol(:);
% use unique to find clusters to be split
urows=unique([grp.T pol],'rows');
% loop over groups
for a=1:max(grp.T)
% only need to split if 2
if(sum(urows(:,1)==a)==2)
% get index of second polarity (ALWAYS POSITIVE)
idx=find(urows(:,1)==a,1,'last');
% find records to reassign
rec=grp.T==urows(idx,1) & pol==urows(idx,2);
% set color (1/2 brightness)
grp.color(max(grp.T)+1,:)=grp.color(a,:)/2;
% reassign
grp.T(rec)=max(grp.T)+1;
end
end
% reset "good" clusters
grp.pop=histc(grp.T,1:max(grp.T));
grp.good=grp.pop>=grp.popcut;
end