5

How do I find the least possible value in Matlab, given the modulo values and its remainder values in an array? for example:

A=[ 23 90 56 36] %# the modulo values
B=[  1  3 37 21] %# the remainder values

which leads to the answer 93; which is the least possible value.


EDIT:

Here is my code but it only seems to display the last value of the remainder array as the least value:

z = input('z=');
r = input('r=');
c = 0;
m = max(z);
[x, y] = find(z == m);
r = r(y);
f = find(z);
q = max(f);
p = z(1:q);
n = m * c + r;
if (mod(n, p) == r)
    c = c + 1;
end
fprintf('The lowest value is %0.f\n', n) 
Eitan T
  • 32,660
  • 14
  • 72
  • 109
Jey
  • 61
  • 1
  • 2
  • 2
    See the "chinese remainder theorem". – Maurits Sep 23 '12 at 11:39
  • There are already several CRT implementations in the MATLAB File Exchange, for instance: [here](http://www.mathworks.com/matlabcentral/fileexchange/32649-chinese-remainder-theorem-for-integers-simple) and [here](http://www.mathworks.com/matlabcentral/fileexchange/5851)... – Eitan T Sep 23 '12 at 12:16
  • @EitanT can you please help me in making this program? i really suck at programming and we are only allowed to use rem and mod commands and i'm not gaining any progress – Jey Sep 23 '12 at 12:31
  • @Jey [This question](http://stackoverflow.com/questions/12544240/least-possible-value-as-described-by-modulo-condition-matlab) seems to be identical to yours. Also, it would be best if you'd have something done (embed that code into the original question) so that it can be improved into a working program. – Eitan T Sep 23 '12 at 12:37
  • @EitanT: If you realize a question is a duplicate, please consider casting a close vote. – Jonas Sep 23 '12 at 12:51
  • @Jonas i'm sorry i thought it's not a duplicate because the other question does not allow rem and mod while mine only allows rem and mod commands – Jey Sep 23 '12 at 13:06
  • @Jonas I've reached my daily limit already :) @ Jey, if you're still stuck, please update this question and paste your code into it. It would be easier to help you when you've got something going already... – Eitan T Sep 23 '12 at 13:13
  • @EitanT okay i'll update it later! but i'm kinda ashamed of my program because its really bad because of my poor MATLAB skills – Jey Sep 23 '12 at 13:17
  • 1
    @Jey Don't be! You cannot get better if you don't try. – Eitan T Sep 23 '12 at 13:25
  • z=input('z='); r=input('r='); c=0; m=max(z); [x,y]=find(z==m); r=r(y); f=find(z); q=max(f); p=z(1:q); n=m*c+r; if (mod(n,p)==r) c=c+1; end fprintf('The lowest value is %0.f\n',n) Here is my code but it only seems to display the last value of the remainder array as the least value @EitanT – Jey Sep 23 '12 at 13:52
  • End a line with two spaces to add a
    linebreak doesn't seem to work
    – Jey Sep 23 '12 at 13:56
  • @Jey
    tags don't work in comments because formatting in comments is somewhat limited. I've pasted your code into your original question itself.
    – Eitan T Sep 23 '12 at 14:09
  • @EitanT thanks! but if you input the values in the first question it only produces 21 as the least value which is the last number in the B array – Jey Sep 23 '12 at 14:11

1 Answers1

3

Okay, so your goal is to find the smallest x that satisfies B(i) = mod(x, A(i)) for each i.

This page contains a very simple (yet thorough) explanation of how to solve your set of equations using the Chinese Remainder Theorem. So, here's an implementation of the described method in MATLAB:

A = [23, 90];                                  %# Moduli
B = [1, 3];                                    %# Remainders

%# Find the smallest x that satisfies B(i) = mod(x, A(i)) for each i
AA = meshgrid(A);
assert(~sum(sum(gcd(AA, AA') - diag(A) > 1)))  %# Check that moduli are coprime
M = prod(AA' - diag(A - 1));
[G, U, V] = gcd(A, M);
x = mod(sum(B .* M .* V), prod(A))

x =
    93

You should note that this algorithm works only for moduli (the values of A) which are coprime!

In your example, they are not, so this will not work for your example (I've put an assert command to break the script if the moduli are not coprime). You should try to implement yourself the full solution for non-comprime moduli!

P.S
Also note that the gcd command uses Euclid's algorithm. If you are required to implement it yourself, this and this might serve you as good references.

Eitan T
  • 32,660
  • 14
  • 72
  • 109