Matlab help plese: Modulo wrap around.

  • Context: MATLAB 
  • Thread starter Thread starter Beer-monster
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around implementing a wrap-around feature in MATLAB for accessing elements in an NxN matrix. Participants are addressing issues related to indexing, particularly how to handle edge cases when reading neighboring elements from the matrix.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their intention to use the mod command to achieve wrap-around behavior when accessing matrix elements at the edges.
  • Another participant suggests modifying the indexing approach to account for MATLAB's one-based indexing, proposing a formula to correctly wrap indices.
  • A participant expresses confusion regarding the indexing and the results they are receiving, indicating they are new to MATLAB.
  • There are corrections and clarifications regarding the use of the mod function and the necessity of adjusting indices to maintain compatibility with one-based indexing.
  • Participants discuss the implications of zero-based versus one-based indexing, with one participant asserting that zero-based indexing is superior.
  • There is a back-and-forth regarding the correct application of the mod function and adjustments needed to avoid negative or zero indices.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to implement the wrap-around feature, and there are multiple competing views regarding the correct indexing methods in MATLAB.

Contextual Notes

Some participants express uncertainty about the correct application of the mod function in relation to MATLAB's indexing system. There are unresolved issues regarding the handling of negative or zero indices, as well as the implications of switching between zero-based and one-based indexing.

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 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
4K