From the looks of it, the whole code is dependent upon sorting things by groups of 10. It's hard to follow the full code without knowing what it's purpose is or how things are sorted based on the "if b<=6" condition. Here are some tips to improve this code, then you can think about how to modify it for variable sizes.
- Define "10" one time, then call that variable wherever you call "10" elsewhere in the code. If 10 is the number of rays, then you might write: nRays = 10.
- Line 43 (r = c/10) requires that dimension 1 of your array is an integer multiple of 10.
- Lines 64 and 69 are the same. Both quantities (X, x) are updated in the if-else statement, so you can move this outside of if-else statement.
- The u; lines do no do anything.
- You can probably vectorize the inner loop for y=2:length(B) by using b = diff(B).
- Look at all of the squiggly lines and tips that MATLAB gives you when the code is open in the Editor and follow the tips to improve your code.