Accessing empty array element through a loop

I have following program
r = 0;
syms x y z
Base = x*y*z;
for j = 1:8
r = r+1;
X(r) = j;
x = X(r);
AN = eval(Base);
Base1(r) = AN;
end
First_Int = (Base1(1,1)+ 4*Base1(1,2)+ 2*Base1(1,3) + 4*Base1(1,4)+ 2*Base1(1,5)+ 4*Base1(1,6)+ 2*Base1(1,7)+ 4*Base1(1,8)+Base1(1,9))/(3);
It is clear that array size is increasing dynamically and consume much time. There will be two other loops for y and z to complete the calculation with values.
Can I have an alternative way of doing this to save time and memory?

回答(1 个)

Harsha - I'm not clear on how your question title accessing empty array element relates to your code. Where are you trying to access an empty element?
If you are concerned with the array size increasing dynamically on each iteration of the loop, then pre-allocate or size the array before entering the loop as
n = 8;
X = zeros(n,1);
Base1 = zeros(n,1);
for k=1:n
X(k) = k;
Base1(k) = k*y*z;
end
It may be that you need to show your outer loops where y and z are initialized. Note how in the above, I have removed the need for the indexing variable r as k can do the same. I use k as MATLAB uses i and j to represent the imaginary number.

5 个评论

My meaning with empty array is X(r), which expands in every iteration without predetermining. To make it simple I did not include my real base function which is exp(-kT*sqrt((x)^2+(y)^2+(z)^2))*kT*(x). In this case it is certainly need evaluation in every iteration. Furthermore, I introduced 'r' variable such way that the value 'j' can be even fractions. What I want is that to define an array before the iteration. I have tried what you said. It ends up with following error.
"??? The following error occurred converting from sym to double: Error using ==> sym.double at 29 DOUBLE cannot convert the input expression into a double array. If the input expression contains a symbolic variable, use the VPA function instead."
You would have to show all of your code so that I can see where the error is being generated. Based on the sample of code that you have shown, I don't think that you need to use symbols for your variables so you could remove the sym.
n=8;
p=0;
q=0;
r=0;
s=0;
t=0;
u=0;
syms xi xj yi yj zj zi
Base_Function = exp(-kT*sqrt(((xi-xj)^2+(yi-yj)^2+(zi-zj)^2)))*kT*kT/((xi-xj)^2+(yi-yj)^2+(zi-zj)^2);
for i = x0:(x1-x0)/n:x1;
p = p+1;
Xi(p) = i;
xi = Xi(p);
Base1(p) = eval(Base_Function);
end
First_Int = (x1-x0)*(Base1(1)+ 4*Base1(2)+ 2*Base1(3) + 4*Base1(4)+ 2*Base1(5)+ 4*Base1(6)+ 2*Base1(7)+ 4*Base1(8)+Base1(9))/(3*n);
continues six loops till all the variables are calculated
Harsha - what is Base1 supposed to be? An array of symbols or scalar values? I don't understand why your are using symbols for xi, xj, yi, yj, zi and zj.
This is a procedure to perform numerical integration using Simpson's rule. Base1 is defined in the loop, which consists of evaluated values of Base_function with respect to xi variable. Just consider this as a integration whose values of variables will be evaluated at each step.

请先登录,再进行评论。

提问:

2016-2-21

评论:

2016-2-28

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by