Iterate through an excel file and perform vector operations

I am trying to iterate through an excel file and apply an equation involving subtracting a vector made from the excel file and a constant vector. I am fairly new to MATLAB and have an idea but would like to be pointed in the right track. I was thinking of using a for loop but not sure on the syntax. Any help would be appreciated.

3 Comments

For more clarification I am trying to find the absolute potential and electric field from an observer to a point charge. The obersever is located at 1x-3y+4z. The locations of the point charges are in the excel file.
doc xlsread % get the data
After that you likely do not need any loops. Have you worked thru the beginning examples in the "Getting Started" section of the online documentation to see how matrix operations work in Matlab?
I have an I have imported the excel file into matlab and the x,y,z locations are in there own separate matrices I just need to apply the equation to all positions and sum them up. For example, the observer is located at 1x-3y+4z and a point charge is located 3x-5y+3z. I know to get that I would just do the x(observer)-x(pointcharge) y(observer)-y(pointcharge) and so forth but would that only give me one position? Also how would I find the magnitude of this vector? I can attach the files if that makes it easier.

Sign in to comment.

 Accepted Answer

dpb
dpb on 14 Oct 2014
Edited: dpb on 15 Oct 2014
That's ok (altho I'd probably not use separate variable but an array).
If the arrays are x, y, z and the constant point is px, etc., ...
>> x=1;y=-3;z=4;px=3;py=5;pz=3;
>> dx=bsxfun(@minus,x,px);
dy=bsxfun(@minus,y,py);
dz=bsxfun(@minus,z,pz);
>> r=sqrt(dx.^2+dy.^2+dpz).^2)
r =
8.3066
>>
As noted you could reduce the number of equations by working with the positions as an array and a vector instead of separate operations...
d=[x y z].-repmat([px py pz],length(x),1);
r=sqrt(sum(d.^2,2));
Now the three columns of d are x,y,z, respectively, of course and r is still a vector.
ADDENDUM
With the understanding that Q is same size as r, then the summation of the formula is simply
res=sum(Q./(4*pi*epsilon*r));
so the whole solution looks like
d=[x y z].-repmat([px py pz],length(x),1);
r=sqrt(sum(d.^2,2));
res=sum(Q./(4*pi*epsilon*r));
which is why I asked initially if you had worked thru "Getting Started" section where it illustrates array operations and the "dot" operators and the like...
If you still hadn't I commend it to your attention sooner rather than later.

7 Comments

Ok that makes sense to get the R part of the equation. Now how would take the elements of the Q vector and put them into an equation using Q and r. The equation is V=Q/(4*pi*epsilon*r). Epsilon is a constant 8.85e^-10. I need to then sum them up. Would I just go v=Q/4*pi*epsilon*r then sum(v)?
I tried this out actually but it seems to be only returning the last value in the vector
for i=length(Q)
for j=length(r)
v=Q(i)/(4*pi*epsilon*r(j));
ans=sum(v)
end
end
is length the wrong thing to use in this scenario?
You overwrite v every pass thru the loop and it's only a single value so sum(v)=v, too.
Q here is a source I presume? Seems peculiar you'd want to sum over them; do you want four separate sums over the distances for each Q or really only a single sum total?
Q is a 1x10 array with positive and negative numbers. I would like to access each element of the array and use it in the equation as well as its corresponding r value. Then I would like it to return the answer for each iteration and add them together. For example
v=sum(Q(1)/(4*pi*epsilon*r(1))) + v=sum(Q(2)/(4*pi*epsilon*r(2)))...
so on until iteration 10
When I use this for code
for i=length(Q)
for j=length(r)
v=Q(i)/(4*pi*epsilon*r(j));
v=sum(v)
end
end
it gets the right value every 10 iterations. How do I get it to only display the correct answers and then sum them all together
I got it
answer=zeros(1,10);
for i=1:length(Q)
v=Q(i)/(4*pi*epsilon*r(i));
answer(i)=v;
end
answer=sum(answer)
Oh, so there's a Q for each r? Somehow I go the idea they were two separate things. That point was what I was trying to clarify.
See the updated Answer for "the Matlab way"...

Sign in to comment.

More Answers (0)

Products

Asked:

on 14 Oct 2014

Edited:

dpb
on 15 Oct 2014

Community Treasure Hunt

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

Start Hunting!