0

As you know, using loops with big size matrix results delay, in my case I need to free my code of loops for the same reason(delay). This is my code example:

for i=1:n
    for j=1:m-1
       Z=A(i,j);
       Z2= A(i,j+1);

       if X(Z,Z) == X(Z2,Z)
          X(Z2,Z) = X(Z2,Z)+1;
       end

     end
 end

So, are there any suggestion? Many thanks.

James
  • 53
  • 10
Mark
  • 11
  • 1
  • 3
  • 2
    Is this a working code? Do you get the output you want from it? Is `A` an array of integers representing indices in `X`? And what is `X`? Can you post some small sample of `A` and `X`? – Stewie Griffin Dec 15 '13 at 12:33
  • 1
    since you are updating `X` as well as testing its values at each iteration the output is likely to depend on the order of the loop (and `A`) therefore any vectorization may SIGNIFICANTLY change the output. – Shai Dec 15 '13 at 13:05
  • 1
    @Mark BTW, it is best [not to use `i` and `j` as variable names in Matlab](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab). – Shai Dec 15 '13 at 13:07
  • Hello Robert ,thank you for responding , yes , I got results from this code, this code compares two values if they are identical , the code add one to one of the values to make it different from the other one , these are the inputs and the results: A=[1 4] [ 1 5] [ 1 6] [ 2 8]; X=[1 0 ] [ 0 1 ] [ 0 0 ] [ 1 0 ] [ 1 0 ] [ 1 0 ] [ 0 0 ] [ 0 1 ]; Results: [ 1 0] [0 1] [0 0] [2 0] [2 0] [2 0] [0 0] [0 2]; – Mark Dec 15 '13 at 13:12
  • Thank you Shai,I agree with you. Thanks for help. – Mark Dec 15 '13 at 13:15

0 Answers0