-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRealignDataByMask.m
49 lines (47 loc) · 1.28 KB
/
RealignDataByMask.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
%% Àëãîðèòì íàõîæäåíèÿ MAP-îöåíêè
%---input---------------------------------------------------------
%
%---output--------------------------------------------------------
%
function [realigned_data, prepadding, postpadding]=RealignDataByMask(data, mask)
dim_num = ndims(mask);
offset = ones(dim_num,1);
rms = ones(1, dim_num) .* size(mask);
for i=1:dim_num
for j=1:size(mask, i)
C = repmat({':'},1,dim_num);
C(i) = {j};
slice = mask(C{:});
if sum(slice.^2) == 0
offset(i) = j+1;
else
break;
end
end
for j=size(mask, i):-1:1
C = repmat({':'},1,dim_num);
C(i) = {j};
slice = mask(C{:});
if sum(slice.^2) == 0
rms(i) = j-1;
else
break;
end
end
end
dim_num_data = ndims(mask);
if ndims(data) ~= ndims(mask)
dim_num_data = ndims(data);
end
C = repmat({':'},1,dim_num_data);
prepadding = zeros(1, dim_num);
postpadding = zeros(1, dim_num);
for i=1:dim_num
C(i) = {str2num(strcat(num2str(offset(i)), ':', num2str(rms(i))))}; %#ok<ST2NM>
prepadding(i) = offset(i) - 1;
postpadding(i) = size(mask, i) - rms(i);
end
if ndims(data) ~= ndims(mask)
C(ndims(data)) = {':'};
end
realigned_data = data(C{:});