Code covered by the BSD License

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

### Highlights from SUB2IND4UP Linear index from subscripts of upper triangular matrix (only elements above diagonal)

3.0
3.0 | 1 rating Rate this file 0 Downloads (last 30 days) File Size: 1.82 KB File ID: #27447 Version: 1.2

# SUB2IND4UP Linear index from subscripts of upper triangular matrix (only elements above diagonal)

### Liber Eleutherios (view profile)

01 May 2010 (Updated )

Single index for given subscripts of a 2D upper matrix, (no elements over the diagonal)

File Information
Description

IND = SUB2IND4UP(I, J) returns the linear index equivalent to the row and column subscripts I and J
Let ind be a vector of indexes for entries of some upper triangular matrix. The entries are selected vertically so that:

ind = 1 is associated to entry (1, 2)
ind = 2 is associated to entry (1, 3)
ind = 3 is associated to entry (2, 3)
ind = 4 is associated to entry (1, 4)
...
ind = N * (N - 1) / 2 is associated to entry (N - 1, N)

***********************************************************************

EXAMPLE

If
A = rand(10);
and
b = A(find(triu(A, 1)));
then, given subscripts
I = [1:9];
and
J = [2:10];
for matrix A, these are equivalent to indices
IND = sub2ind4up(I, J);
for vector b. In fact:
all(A(sub2ind(size(A), I, J)) == b(IND)')

ans =
1

This is obtained without even knowing about size(A)

***********************************************************************

See also SUB2IND, IND2SUB, FIND.

MATLAB release MATLAB 7 (R14)
MATLAB Search Path
`/`
Tags for This File   Please login to tag files.
 Please login to add a comment or rating.
Comments and Ratings (2)
03 May 2010 Bruno Luong

### Bruno Luong (view profile)

Why restrict on one level upper diagonal? It is too restrictive. It should be able to handle lower and upper, with all the shift, positive or negative.

03 May 2010 Liber Eleutherios

### Liber Eleutherios (view profile)

% Note that if
A = rand(10);
% and
b = A(find(triu(A, 1)));
% then, given subscripts
I = [1:9];
% and
J = [2:10];
% for matrix A, these are equivalent to
IND = sub2ind4up(I, J);
% for vector b. Infact:
all(A(sub2ind([10, 10], I, J)) == b(IND)')
%
% ans =
%
% 1
%
% This is obtained without even knowing about size(A).

Comment only
Updates
03 May 2010 1.1

Just changed the example in order to clarify how the function is supposed to be used.

12 Jan 2015 1.2

minor edits

Contact us