Code covered by the BSD License  

Highlights from
index.m - create a list of the indices and values of an array

Be the first to rate this file! 3 Downloads (last 30 days) File Size: 1.98 KB File ID: #21330
image thumbnail

index.m - create a list of the indices and values of an array

by Georg Stillfried

 

04 Sep 2008 (Updated 26 May 2009)

Create a list where each value (content) of an array is displayed together with its index.

| Watch this File

File Information
Description

This function displays each value of an array together with its index. It is possible to ignore zeros and/or NaNs and/or to sort the list according to value. The values can be integers or floating point numbers.

EXAMPLES:

>> a=[2,3,NaN;1,2,4;7,0,9];

>> index(a,'sorted')

ans =
     3 2 0
     2 1 1
     1 1 2
     2 2 2
     1 2 3
     2 3 4
     3 1 7
     3 3 9
     1 3 NaN

>> index(a,'sorted','nonans','nozeros')

ans =
     2 1 1
     1 1 2
     2 2 2
     1 2 3
     2 3 4
     3 1 7
     3 3 9

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
05 Sep 2008 Jos x@y.z

I suggest you look at IND2SUB, which allows for easy vectorization
% START OF CODE
a = [3 2 1 ; 7 0 1 ; NaN -1 Inf] ; % some data
[ind{1:ndims(a)}] = ind2sub(size(a),1:numel(a)) ;
r = [cat(1,ind{:}).' a(:)]
% END OF CODE
  
and work from that onwards ...
On the good side, at least you provided some help, but take care of a proper H1 line used by LOOKFOR.

26 May 2009 Georg Stillfried

Thank you Jos. Your suggestion is now incorporated in an improved version.

Please login to add a comment or rating.
Updates
05 Sep 2008

Changed summary to fit into the summary field.

05 Sep 2008

improved speed for large arrays

26 May 2009

faster through ind2sub

Tag Activity for this File
Tag Applied By Date/Time
matrices Georg Stillfried 22 Oct 2008 10:17:44
index Georg Stillfried 22 Oct 2008 10:17:44
indexes Georg Stillfried 22 Oct 2008 10:17:44
indices Georg Stillfried 22 Oct 2008 10:17:44
array Georg Stillfried 22 Oct 2008 10:17:44
sorted Georg Stillfried 22 Oct 2008 10:17:44
reshape Georg Stillfried 22 Oct 2008 10:17:44
list Georg Stillfried 22 Oct 2008 10:17:44
matrix Georg Stillfried 22 Oct 2008 10:17:44
sort Georg Stillfried 27 May 2009 09:55:03

Contact us at files@mathworks.com