File: FindInd.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (41 lines) | stat: -rw-r--r-- 990 bytes parent folder | download | duplicates (6)
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
function varargout = FindInd(in, k, mode)
% find that also does higher dims for output like [x,y,x,t] = findfull(4-D)
% (unlimited of course)
%
% steal from MinInd, and then edit MinInd and MaxInd

% DN 2008-07-29 Wrote it

% input/output checking
if nargin == 3
    switch mode
        case 'first'
            qfirst = true;
        case 'last'
            qfirst = false;
        otherwise
            error('mode %s not recognized',mode)
    end
elseif nargin == 2
    qfirst = true;
end

psychassert(nargout==0 || nargout==1 || nargout==ndims(in),'number of outputs must be one or equal to the number of dimensions of the input')

% do the work
inds                = find(in);

if nargin==2 || nargin==3
    if qfirst
        inds    = inds(1:k);
    else
        inds    = inds(end-k+1:end);
    end
end
    
[varargout{1:ndims(in)}]    = ind2sub(size(in),inds(:));

% output
if nargout==0 || nargout==1
    varargout   = {[varargout{:}]};
end