# Caculating the sum of elements and building a vector and a matrix with letters

1 view (last 30 days)
Lee Cohen on 7 Apr 2018
Commented: John BG on 8 Apr 2018
I created the following matrix: Here's the code:
n = input('enter n\n');
A=[sym('m%d', [n 1]) sym('x%d', [n 1]) sym('y%d', [n 1]) sym('z%d', [n 1])];
I need to build the following vector and matrix: Once with using loops and once without using loops. I don't succeed to create a sum of elements, that are letters. How Can I build those arrays?

John BG on 7 Apr 2018
Hi Lee
1.-
Simulating data
n=12
m=randi([1 10],n,1);
x=randi([-13 13],n,1);
y=randi([-13 13],n,1);
z=randi([-13 13],n,1);
2.-
A=[m x y z]
3.-
the v vector
v=[sum(m.*x) sum(m.*y) sum(m.*z)]
it's the same as
v=[sum(A(:,1).*A(:,2)) sum(A(:,1).*A(:,3)) sum(A(:,1).*A(:,4))]
4.-
Regarding the Inertia matrix I, once you have keyed in the matrix A with the couple lines shown in your question
n = input('enter n\n');
A=[sym('m%d', [n 1]) sym('x%d', [n 1]) sym('y%d', [n 1]) sym('z%d', [n 1])];
the generatrion of I is more compact using vectors m x y and z
To get these vectors one can easily extract m x y z with
m=A(:,1);
x=A(:,2);
y=A(:,3);
z=A(:,3);
then, note that it may be the case that you need I to be 3D so you can address the Inertia moment for any given i-th layer
I0=zeros(3,3,n)
% 1st row of I
I0(1,1,:)=m.*(x).^2
I0(2,1,:)=m.*x.*y
I0(3,1,:)=m.*x.*z
% 2nd row of I
I0(1,2,:)=m.*x.*y
I0(2,2,:)=m.*(y).^2
I0(3,2,:)=m.*z.*y
% 3rd row of I
I0(1,3,:)=m.*x.*z
I0(2,3,:)=m.*y.*z
I0(3,3,:)=m.*(z).^2
5.-
then one can obtain the sum matrix I just adding along 3rd dimension of I0
I=sum(I0,3)
thanks in advance for time and attention
John BG
John BG on 8 Apr 2018
Any time, happy to help :)