MATLAB: Error inserting symbolic expression in for-loop

for loop

A have some expressions outside a loop and want to use them inside the loop like
p=P(i,j) %some expressions that must be calculated outside the loop

q=Q(i,j) %some expressions that must be calculated outside the loop
for i=1:10
for j=1:10
W(i,j)=P+i+j
V(i,j)=Q+i-j
end
end
i keep recieving this error:
The following error occurred converting from sym to double:
Unable to convert expression into double array.

Best Answer

Your P or Q appear to be a symbolic function, and you appear to be invoking them with specific arguments i and j to produce p and q. But then you use the full P and Q functions inside the loop instead of p and q. With P and Q being symbolic functions, it would not be possible to convert the right hand side to double. It might be possible to convert p and q to double.
If it turns out not to be possible to convert p and q to double, then you will need to initialize W and V to symbolic instead of numeric. It could be the case that you had previously used W and V in your calculations and so even though you might be overwriting each entry of W and V, that would have left them lying in memory as numeric.
W = zeros(10, 10, 'sym');
V = zeros(10, 10, 'sym');