You need to explain a lot better what you want to do, what you mean by 'grid', what you mean by 'case', what your inputs are, etc.
The code you've posted is nonsensical, possibly because the poorly named i variable is used for different purpose.
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!