help-octave
[Top][All Lists]

## something like a modulo function (but not)?

 From: E. Joshua Rigler Subject: something like a modulo function (but not)? Date: Wed, 27 Oct 2004 11:15:09 -0600

```I feel a little silly even asking this question, but since when has that
stopped me :^)?  I have a vector of sequential indices to a 2-D matrix
that I want to convert to a set of column-wise vector indices.  For
example a particular 5x5 matrix would be fortran indexed as:

mtrx = [ 1   6  11  16  21 ]
[ 2   7  12  17  22 ]
[ 3   8  13  18  23 ]
[ 4   9  14  19  24 ]
[ 5  10  15  20  25 ]

I extract a subset of this matrix that gives me the following vector of
indices:

idx1 = [ 3   5   8  10  13  15  18  20  23  25 ]

and wish to convert it to something like:

idx2 = [ 3   5   3   5    3   5   3    5   3   5 ]

The first thing that popped into my head was that I would need to use
the modulo function in Octave (i.e., idx2=mod(idx1,5)), but of course
that would give me:

idx2 = [ 3   0   3   0    3   0   3    0   3   0 ]

I guess what I really want is something similar to modulo, except where
mod(n,I*n)=n,  not zero ("I" is an integer).  The best solution I could
come up with on my own is:

octave:44> idx2 = mod(idx1,5)+(mod(idx1,5)==0)*5
idx2 =

3  5  3  5  3  5  3  5  3  5

Can anyone out there suggest something a little more elegant that would
have less impact on a long and already computationally intensive
iterative function?  Am I just being dense?

-EJR

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```