File Exchange

image thumbnail

FIND for n-D: Find indices and values of nonzero elements in n-D matrix

version 1.0 (971 Bytes) by

FINDN is the FIND function for n-D matrix



No License

Just like the FIND function in Matlab, FINDN is a simple function which will find the indices and values of nonzero elements in a n-D matrix

function [sub v] = findn(A)
% INPUT: A: n-D matrix
% OUTPUT: sub: the k x n index matrix (k is the number of the nonzeros)
v: a column or row vector v of the nonzero entries in A
       [sub ] = findn(A)

    A = zeros(4,3,5,7);

      Brian H. Hui (
       PhD candidate, Electrical Engineering, University of Southern California

Comments and Ratings (5)

I was trying something like x{1:length(L)}= ind2sub(L,IDX); but it gave error....thanks for posting the code. Still I wonder why my stat didn't work


us (view profile)

now, since others have backed-up my idea, you should soon come-up with an updated version...

H. Brian Hui

Thanks Matt. Did not realize ind2sub can return n subscript arrays.


Loginatorist (view profile)

This seems a little faster for large number of dimensions:

IDX = find(A);
L = size(A);
[x{1:length(L)}] = ind2sub(L,IDX);
sub = [x{:}];
if nargout==2
    v = A(IDX);

Something to consider.


us (view profile)

why not simply

% the data
% the engine (in a function)
% the result
     1 1 1 1 1
     2 2 2 2 2
     1 2 3 4 5

just a thought...

MATLAB Release
MATLAB 7.6 (R2008a)
Tags Add Tags

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video