/* Written by: -- John L. Weatherwax 2006-05-30 email: wax@alum.mit.edu Please send comments and especially bug reports to the above email address. ----- */ #include "sub2ind.h" /* A block row version -- compatable with C's conventions if M=4 & N=3 then given a subscript (the pair of numbers in the below) this routine returns the first number below 0=(0,0) 1=(0,1) 2=(0,2) 3=(1,0) 4=(1,1) 5=(1,2) 6=(2,0) 7=(2,1) 8=(2,2) 9=(3,0) 10=(3,1) 11=(3,2) */ int sub2ind(int M, int N, int i, int j){ return i*N+j; } void test_sub2ind(void){ int M,N,ind,i,j; M=4; N=3; for( i=0; i < M; i++ ){ for( j=0; j < N; j++ ){ ind = sub2ind(M,N,i,j); printf("(i,j)=(%d,%d); ind=%d\n",i,j,ind); } } }