Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab modulo specific behaviour

Tags:

matlab

I have the following problem. Let's say I have four possible values {1 2 3 4} and I want a specific behavior of mod function

The behavior I seek is this one

 1 mod 4 = 1
 2 mod 4 = 2
 3 mod 4 = 3
 4 mod 4 = 4

but I have the following results with matlab.

 1 mod 4 = 1
 2 mod 4 = 2
 3 mod 4 = 3
 4 mod 4 = 0

Are there any ideas as how to achieve the desired behavior with the simplest way possible in MATLAB?

like image 623
Wanderer Avatar asked Dec 11 '22 21:12

Wanderer


1 Answers

If A holds those values, you can subtract 1, perform mod and add back 1.

Sample run -

>> A = 1:8
A =
     1     2     3     4     5     6     7     8
>> mod(A-1,4)+1
ans =
     1     2     3     4     1     2     3     4
like image 160
Divakar Avatar answered Dec 22 '22 07:12

Divakar