Caculating the sum of elements and building a vector and a matrix with letters
2 次查看(过去 30 天)
显示 更早的评论
I created the following matrix:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/171924/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/171925/image.png)
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?
0 个评论
采纳的回答
John BG
2018-4-7
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.-
Your matrix
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)
f you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!