function [r, c] = i2rc(idx, nr, nc)
% Convert the index of a vector (of length nr*nc) to the 
%   row and column numbers of an nr-by-nc 2-d array.  
%   Assume the 1-d array is mapped to 2-d column-wise.
% idx is a vector of 1-d array indices such that
%    idx(k) maps to the row and column numbers r(k),c(k)

for k= 1:length(idx)
    c(k)= ceil(idx(k)/nr);     % column number
    r(k)= rem(idx(k)-1,nr)+1;  % row number
    %fprintf('%2d   %d,%d\n', idx(k), r(k), c(k))  % for testing
end

