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

2 ビュー (過去 30 日間)
Lee Cohen
Lee Cohen 2018 年 4 月 7 日
コメント済み: John BG 2018 年 4 月 8 日
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
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
  6 件のコメント
Lee Cohen
Lee Cohen 2018 年 4 月 8 日
Thank you so much!
John BG
John BG 2018 年 4 月 8 日
Any time, happy to help :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by