MATLAB Matlab help plese: Modulo wrap around.

  • Thread starter Thread starter Beer-monster
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
The discussion focuses on implementing a wrap-around feature in a code that reads elements from an NxN matrix, particularly addressing issues when accessing elements at the matrix's edges. The initial approach involved using the mod function to handle edge cases, but errors occurred due to incorrect indexing. The key solution involves adjusting the indexing to account for MATLAB's one-based indexing system. Specifically, the correct formula for accessing neighboring elements is to first convert to zero-based indexing by subtracting one, apply the mod function to ensure the index wraps around, and then convert back to one-based indexing by adding one. This method ensures that accessing elements beyond the matrix boundaries correctly retrieves the first element in the respective row or column. The discussion concludes with an acknowledgment of understanding the indexing adjustments necessary for proper functionality.
Beer-monster
Messages
285
Reaction score
0
*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
 
Physics news on Phys.org
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.
 
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.
 
Matlab has 1 based indexing for matricies, cells, and structure arrays, and related funcitons.


Beer-monster said:
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.
 
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.)
 
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:
Got it. Thank you very much. Now to loop the whole thing.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K