Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab help plese: Modulo wrap around.

  1. Nov 1, 2011 #1
    *apologies for spelling error in title*


    I'm writing a code that first reads a random element of an NxN matrix and then reads out the nearest neighbouring elements. However, if the first element chosen at the edge of the matrix the code then tries to read an element that is not there and flags and error.

    I'd like to introduce a wrap-around so that if the code reads of the edge of a row/column is reads the first element in that row column.

    i.e if the code chooses the element 5,N and tries to read the element 5,N+1 it will return the value in the element 5,1.

    I thought I could accomplish this by adding the mod command in my matrix reference, but it doesn't seem to be working. Could someone please tell me what I'm doing wrong.

    The relevant part of the code is shown below. The code has already chosen the element in (B(1), B(1)) in matrix A (B is a reference vector).


  2. jcsd
  3. Nov 1, 2011 #2
    Assuming B is an index for one based indexing, I think instead you should have

    Code (Text):

    c=mod(B(1) - 1,N) + 1
    This way N->N, N+1 -> 1

    This could serve as evidence that zero based indexing is superior.
  4. Nov 1, 2011 #3
    I believe it is a 1 based index as the error flag I receive states that the index must be a positive integer. Is there a way to change that in matlab?

    Unfortunately, I tried as below and got a zero or negative in return. Is there something I'm missing.


    I put the modulus directly into the index this time so I could see how it worked trying to go off either edge of the matrix.
  5. Nov 1, 2011 #4
    Matlab has 1 based indexing for matricies, cells, and structure arrays, and related funcitons.

    The first line should not have mod(B(1)-1,N)-1 but rather mod(B(1)-1,N)+1. I don't know if that was just a typing error or if you thought you had some reason for putting a -1 after the mod function call, but it's wrong to have it there.
  6. Nov 1, 2011 #5
    Sorry, I got confused about the purpose of the -1 at the end.

    So I tried.


    However, it's still giving me negative or zero indices. Is there another mistake, (I'm still kind of new to matlab, sorry.)
  7. Nov 2, 2011 #6
    I instructed you to replace the second -1 with +1 on the first line, but you did something else. The two lines should be

    Code (Text):
    The purpose of the +1 is to convert back to 1 based indexing. The semicolons are optional, to repress output.

    subtract one to convert to zero based index
    perform modulus to get remainder after division by N - results in an index in the interval from 0 to N-1
    add one - results in an index in the interval from 1 to N
    Last edited: Nov 2, 2011
  8. Nov 2, 2011 #7
    Got it. Thank you very much. Now to loop the whole thing.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook