forked from johncolby/along-tract-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrk_length.m
43 lines (39 loc) · 1.26 KB
/
trk_length.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
function lengths = trk_length(tracks)
%TRK_LENGTH - Calculate the lengths of tracks
%
% Syntax: lengths = trk_length(tracks)
%
% Inputs:
% tracks - TrackVis track group. For tracks all of the same length (i.e.
% TRK_INTERP has been run), either structure or matrix form is fine.
%
% Outputs:
% lengths [1 x nTracks]
%
% Example:
% [header tracks] = read_trk(trkPath);
% lengths = trk_length(tracks);
% mean(lengths), std(lengths)
%
% Other m-files required: trk_restruc
% Subfunctions: none
% MAT-files required: none
%
% See also: TRK_READ
% Author: John Colby ([email protected])
% UCLA Developmental Cognitive Neuroimaging Group (Sowell Lab)
% Apr 2010
% Put in matrix form if possible
if isstruct(tracks) && length(unique(cat(tracks.nPoints)))==1
tracks = trk_restruc(tracks);
end
% Fast matrix operation if all tracks are the same length
if isnumeric(tracks)
lengths = sum(sqrt(squeeze(sum((tracks(2:end,1:3,:) - tracks(1:(end-1),1:3,:)).^2, 2))), 1);
% Slow forloop if tracks are not the same length
else
lengths = zeros(1,length(tracks));
for i=1:length(tracks)
lengths(i) = sum(sqrt(squeeze(sum((tracks(i).matrix(2:end,1:3) - tracks(i).matrix(1:(end-1),1:3)).^2, 2))), 1);
end
end