Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting between matrix subscripts and linear indices (like ind2sub/sub2ind in matlab)

Tags:

r

matlab

Let's say you have a matrix

m <- matrix(1:25*2, nrow = 5, ncol=5)

How do you go from matrix subscripts (row index, column index) to a linear index you can use on the matrix. For example you can extract values of the matrix with either of these two methods

m[2,3] == 24
m[12] == 24

How do you go from (2,3) => 12 or 12 => (2,3) in R

In Matlab the functions you would use for converting matrix subscripts to linear indices and vice versa are ind2sub and `sub2ind

Is there an equivalent way in R?

like image 565
djiao Avatar asked Dec 15 '10 16:12

djiao


People also ask

What does ind2sub mean in MATLAB?

Description. example. [ row , col ] = ind2sub( sz , ind ) returns the arrays row and col containing the equivalent row and column subscripts corresponding to the linear indices ind for a matrix of size sz .

How does MATLAB calculate linear indices?

Description. k = find( X ) returns a vector containing the linear indices of each nonzero element in array X . If X is a vector, then find returns a vector with the same orientation as X . If X is a multidimensional array, then find returns a column vector of the linear indices of the result.

What are indices in MATLAB?

Indexing into a matrix is a means of selecting a subset of elements from the matrix. MATLAB® has several indexing styles that are not only powerful and flexible, but also readable and expressive. Indexing is a key to the effectiveness of MATLAB at capturing matrix-oriented ideas in understandable computer programs.

How do you find the index of a value in MATLAB?

In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indices and the element from the array. The find() function returns a vector containing the data.


3 Answers

This is not something I've used before, but according to this handy dandy Matlab to R cheat sheet, you might try something like this, where m is the number of rows in the matrix, r and c are row and column numbers respectively, and ind the linear index:

MATLAB:

[r,c] = ind2sub(size(A), ind)

R:

r = ((ind-1) %% m) + 1
c = floor((ind-1) / m) + 1

MATLAB:

ind = sub2ind(size(A), r, c)

R:

ind = (c-1)*m + r
like image 149
JD Long Avatar answered Nov 16 '22 01:11

JD Long


For higher dimension arrays, there is the arrayInd function.

> abc <- array(dim=c(10,5,5))
> arrayInd(12,dim(abc))
     dim1 dim2 dim3
[1,]    2    2    1
like image 30
aaronjg Avatar answered Nov 16 '22 01:11

aaronjg


You mostly don't need those functions in R. In Matlab you need those because you can't do e.g.

A(i, j) = x

where i,j,x are three vectors of row and column indices and x contains the corresponding values. (see also this question)

In R you can simply:

A[ cbind(i, j) ] <- x

like image 21
ggll Avatar answered Nov 16 '22 00:11

ggll