File: FindRepeatAlongDims.m

package info (click to toggle)
psychtoolbox-3 3.0.9%2Bsvn2579.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 63,408 kB
  • sloc: ansic: 73,310; cpp: 11,139; objc: 3,129; sh: 1,669; python: 382; php: 272; makefile: 172; java: 113
file content (45 lines) | stat: -rw-r--r-- 1,384 bytes parent folder | download
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
function inds = FindRepeatAlongDims(in,dim)
% find repeated rows or columns in a matrix.
% inds = FindRepeatAlongDims(in,dim)
% example:
% in =
%      1     2     3
%      2     3     4
%      4     5     6
%      1     2     3
%      1     2     3
%      4     5     6
% FindRepeatAlongDims(in,1)  % check for repeated rows
% ans = 
%      5                    % the fifth row is the same as the fourth row,
%                             repeat!
%
% for N-D dimensions, this function can also find repeats along the highest
% dimension, such as finding repeated planes in a 3D matrix.

% 04-02-09 DN  Wrote it.

psychassert(dim>0 && dim<=ndims(in),'dim input argument outside possible range (1:ndims(in)')
if ndims(in)>2
    psychassert(dim==ndims(in),'if input has more than 2 dimensions, repetitions can only be found along the highest dimension')
end

thediff = diff(in,[],dim);

if dim==1 && ndims(in)==2
    thediff = thediff.';
else
    nrows   = prod(AltSize(thediff,1:dim-1));
    rest    = num2cell(AltSize(thediff,dim:ndims(thediff)));

    thediff = reshape(thediff,nrows,rest{:});
end

thesum = sum(thediff,1);

if isvector(thesum)
    % if we test over the highest dimension, thesum will always be a vector
    % and we can simply return the indices to doubles in that highest
    % dimension
    inds = find(thesum==0)+1;
end