Matlab help plese: Modulo wrap around.

  • #1
296
0

Main Question or Discussion Point

*apologies for spelling error in title*

Hi

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).


c=mod(B(1)+1,N)
s1=L1(c,B(2))
S1=L2(c,B(2))


Thanks
 

Answers and Replies

  • #2
760
69
Assuming B is an index for one based indexing, I think instead you should have

Code:
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.
 
  • #3
296
0
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.

s1=L1((mod(B(1)-1,N)-1),B(2))
S1=L2((mod(B(1)-1,N)+1),B(2))


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.
 
  • #4
760
69
Matlab has 1 based indexing for matricies, cells, and structure arrays, and related funcitons.


s1=L1((mod(B(1)-1,N)-1),B(2))
S1=L2((mod(B(1)-1,N)+1),B(2))

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.
 
  • #5
296
0
Sorry, I got confused about the purpose of the -1 at the end.

So I tried.

s1=L1((mod(B(1)-1,N)-1),B(2))
S1=L2((mod(B(1)+1,N)-1),B(2)))


However, it's still giving me negative or zero indices. Is there another mistake, (I'm still kind of new to matlab, sorry.)
 
  • #6
760
69
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:
s1=L1((mod(B(1)-1,N)+1),B(2));
S1=L2((mod(B(1)-1,N)+1),B(2));
The purpose of the +1 is to convert back to 1 based indexing. The semicolons are optional, to repress output.


summary:
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:
  • #7
296
0
Got it. Thank you very much. Now to loop the whole thing.
 

Related Threads on Matlab help plese: Modulo wrap around.

  • Last Post
Replies
5
Views
922
Replies
2
Views
7K
Replies
3
Views
1K
Replies
2
Views
12K
Replies
2
Views
3K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
1
Views
744
  • Last Post
Replies
2
Views
6K
Top