-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdist_char.m
74 lines (66 loc) · 2.3 KB
/
dist_char.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
%% dist_char
% computes a symmetric matrix of characteristic distances given a symmetric matrix of distances
function cdist = dist_char(dist, taxa)
% created 2021/07/10 by Bas Kooijman
%% Syntax
% mdist = <../dist_mean.m *dist_mean*> (dist, taxa)
%% Description
% computes a symmetric matrix of characteristic distances between selected taxa (non-zero diagonal) given a symmetric matrix of distances of species
% All species that are member of the taxa must be in the species-distances
%
% Input:
%
% * dist: symmetrix square species-distance matrix
% * taxa: (n, 1)-cell array with names of taxa
%
% Output:
%
% * cdist: square symmetric matrix of characteristic taxa-distances, with non-zero diagonal
%% Remarks
% <lineage.html *lineage*> is used to identify the taxon that was used to create the species-distance matrix.
% All members of taxa must be members of this taxon.
% Diagonal elements are only zero for a taxon if that taxon has only 1 member species.
%% Example of use
% dist = dist_traits(select('Squalomorphi', {'p_M';'v';'a_b'}));
% cdist = dist_char(dist,{'Squatiniformes','Pristiophoriformes','Squaliformes','Hexanchiformes'});
% see mydata_seq
% identify taxon that belongs to dist
lin = lineage(taxa{1}); n_lin = length(lin); i = 1; n_dist = size(dist,2);
while i < n_lin
n_taxon = length(select(lin{n_lin - i}));
if n_dist == n_taxon
taxon = lin{n_lin - i};
break
elseif n_dist < n_taxon
fprintf('Warning from dist_mean: no taxon found for distance matrix\n')
cdist = []; return
end
i = i + 1;
end
if i == n_lin
fprintf('Warning from dist_mean: no taxon found for distance matrix\n')
cdist = []; return
end
% identify the species-indices of members of taxa in dist
spec = select(taxon); ind = 1:n_dist;
n_taxa = length(taxa); ind_taxa = cell(n_taxa,1);
for i = 1:n_taxa
speci = select(taxa{i});
ind_taxa{i} = ind(ismember(speci, spec));
if isempty(ind_taxa{i})
fprintf('Warning from dist_mean: taxon %s is not found in distance matrix\n', taxa{i})
cdist = []; return
end
end
% fill the mean distance matrix
cdist = zeros(n_taxa);
for i = 1:n_taxa
for j = i : n_taxa
d = dist(ind_taxa{i}, ind_taxa{j}); % sub-matrix of distances
if isempty(ind_taxa{i}) || isempty(ind_taxa{j})
x=1;
end
cdist(i,j) = char_su(d(:));
cdist(j,i) = cdist(i,j);
end
end