Matlab help plese: Modulo wrap around.

  • MATLAB
  • Thread starter Beer-monster
  • Start date
  • Tags
    Matlab
In summary, the code should use mod(B(1)+1,N) instead of mod(B(1)-1,N) to convert to zero based indexing. This way N->N, N+1 -> 1.
  • #1
Beer-monster
296
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
  • #2
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
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
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.
 
  • #5
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
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
Got it. Thank you very much. Now to loop the whole thing.
 

1. What is the purpose of using the modulo wrap-around function in Matlab?

The modulo wrap-around function in Matlab is used to perform calculations involving circular or periodic data. It allows you to wrap the data around a specified range, rather than continuing to increase or decrease indefinitely.

2. How does the modulo wrap-around function work in Matlab?

The modulo wrap-around function in Matlab uses the % operator to find the remainder after dividing the first number by the second number. This remainder is then used as the output value, resulting in a wrapped calculation.

3. Can the modulo wrap-around function be applied to non-numeric data in Matlab?

No, the modulo wrap-around function in Matlab can only be applied to numeric data. If you attempt to use it with non-numeric data, you will receive an error message.

4. What is the difference between the modulo wrap-around function and the regular modulo function in Matlab?

The main difference is that the regular modulo function in Matlab does not wrap the data around a specified range, but rather returns the remainder as is. The modulo wrap-around function, on the other hand, wraps the remainder within the specified range.

5. Can the modulo wrap-around function be used with negative numbers in Matlab?

Yes, the modulo wrap-around function in Matlab can be used with both positive and negative numbers. It will wrap the data around the specified range regardless of the sign of the input numbers.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
Back
Top