Hmm. You created A as sparse, and all zero. Then you index into A1, which appears to be undefined. I'll assume that you simply cannot type, and meant A there also.
Instead, you need to learn to actually use sparse, instead of trying to stuff elements into a sparse matrix one at a time. READ THE HELP FOR SPARSE! My re-writing that help for you to read cannot possibly be more efficient than you simply reading the help yourself.
help sparse
doc sparse
As far as improving the code you have written that uses a cell array P of completely unknown shape and size, and some completely unknown function of variable u, you probably need to learn to use tools like cellfun, cat, cell2mat, etc. Then make one call to sparse at the end. But I cannot read your mind about things you won't bother to tell us.