Code covered by the BSD License  

Highlights from
IND2SUB4UP Subscripts from linear index for upper triangular matrix (only elements above diagonal)

3.0

3.0 | 1 rating Rate this file 5 Downloads (last 30 days) File Size: 1.81 KB File ID: #27446
image thumbnail

IND2SUB4UP Subscripts from linear index for upper triangular matrix (only elements above diagonal)

by Francesco Pozzi

 

01 May 2010 (Updated 03 May 2010)

Subscripts from a given single index for a 2D upper matrix (no elements over the diagonal)

| Watch this File

File Information
Description

[I, J] = IND2SUB4UP(IND) returns vectors I and J containing equivalent row and column subscripts corresponding to the index vector IND.

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 indices
             IND = [1:45];
for vector b, these are equivalent to subscripts
             [I, J] = ind2sub4up(IND);
for matrix A. 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)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
03 May 2010 Francesco Pozzi

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

03 May 2010 Bruno Luong

Same comment as the other twin package

Please login to add a comment or rating.
Updates
03 May 2010

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

Tag Activity for this File
Tag Applied By Date/Time
subscripts Francesco Pozzi 03 May 2010 11:17:16
linear indexes Francesco Pozzi 03 May 2010 11:17:16
matrix Francesco Pozzi 03 May 2010 11:17:16
vector Francesco Pozzi 03 May 2010 11:17:16
array Francesco Pozzi 03 May 2010 11:17:17

Contact us at files@mathworks.com