Asked by Zeeshan Abbas
on 18 Jun 2019

n = 450*450;

for i = 0:1:n

for j = 0:1:n

i = mod (i+1,n+1);

j = mod (j + S(i+1), n+1);

S([i+1 j+1]) = S([j+1 i+1]);

end

end

I need to run this code for initial sequence S = {1,2,3,...,450*450}, but due to very high computation matlab is not able to do this. Is there any way to acheive this using less calculations?

Answer by Guillaume
on 18 Jun 2019

Edited by Guillaume
on 18 Jun 2019

Even if your code is correct, it is extremely bad practice to use the same variable names for the loop indices and for local variables within the loop. Certainly any experience coder will pause when looking at that code and start to wonder if there's a bug, as Wallter did. Make the life of the reader easier, remove the ambiguity:

n = 450*450;

for i = 0:1:n

for j = 0:1:n

ind1 = mod(i+1, n+1) + 1;

ind2 = mod(j + S(ind1), n+1) + 1;

S([ind1, ind2]) = S([ind2, ind1]); %swap linear indices ind1 and ind2

end

end

In the calculation of ind2 (your original 2nd j), it is unclear if you meant to use the loop index i or the i calculated on the previous line. Your code would have used the i calculated on the previous line, so i have done the same.

Note that your S needs to have n+1 elements for your code to work. Are you sure you didn't mean mod(..., n)?

edit: As for speeding it up, no it's not possible because of the dependence on S(ind1) in the calculation of ind2. And since you want to do swaps = 41,006,655,001 swaps ~= 41 billions swap, it's going to take a loooong time, but matlab will eventually get you the result. It takes about 0.3 seconds on my machine to do one i loop, so in about 18 hours you'd get your result. Do you actually need to do that many swaps?

Zeeshan Abbas
on 18 Jun 2019

Ok please, that would be enough.

And it would be bonus if you can mention some keyed permutation algorithm names and if they have any method to check the randomess of that permuted sequences.

Guillaume
on 18 Jun 2019

Since Walter removed your explanation, I have no idea what the intent of your code is. You can explain it without mentioning encryption.

As I said, as I, there's no way to speed up this code because of the depency on S in the calculation of ind2. if you remove that dependency, then the index calculation can be sped up, but you'll still have to perform 41 billion swaps which is always going to take a while. The only way to significantly speed up the code is by greatly reducing the number of swaps needed. performing 202,501 swaps takes only 0.3 seconds on my machine. But doing these 202,501 swaps another 202,501 times is 202,501 * 0.3 seconds ~= 18 hours!

Zeeshan Abbas
on 18 Jun 2019

I don't need any encryption. Actually I got 'S' using KSA algorithm. As the initial sequence to KSA was {1,2,3,...}, to hide this thing and to get another permutation I an trying to swap all again.

My ultimate target is to find keyed permutations and then to check the randomness of the permuted sequences using any randomness test.

If you can mention any keyed permutations and randomess tests, it will be helpful enough.

Sign in to comment.

Answer by Zeeshan Abbas
on 19 Jun 2019

What if I can make this like:

for i = 0:1:n

% for j = 0:1:n

ind1 = mod(i+1,n+1)+1;

ind2 = mod(i+S(ind1), n+1) +1;

S([ind1 , ind2]) = S([ind2 , ind1])

% end

end

Is it correct?

Walter Roberson
on 19 Jun 2019

Perhaps, but this does something different than the original code.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Walter Roberson (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/467607-making-loop-calculate-able#comment_715744

## Zeeshan Abbas (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/467607-making-loop-calculate-able#comment_715748

Sign in to comment.