Math problem for code

2 ビュー (過去 30 日間)
Mahmoud Sami
Mahmoud Sami 2018 年 4 月 2 日
回答済み: njj1 2018 年 4 月 2 日
I had a matrix S=n*3 (x,y,z) where n is variable change every run and i want to get another variable L1 to Ln where L=sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2) in a matrix contain no of column (n)

採用された回答

njj1
njj1 2018 年 4 月 2 日
Try using diff(S,1,dim) to get the difference between sequential elements in the matrix S. Each entry in diff(S) is (x_{n} - x_{n-1}, y_{n} - y_{n-1}, z_{n} - z_{n-1}). Then you can square each element of diff(S): (diff(S)).^2. Finally, sum and take the square root: sqrt(sum(diff(S)).^2),dim). In each of these cases, dim is the dimension along which the operation is to take place. So, for the sake of discussion, let's assume S is nx3 (n columns and 3 rows).
diffS = diff(S,1,1);
squaredDiffS = diff.^2;
output = sqrt(sum(squaredDiffS,2));
The output of this code is (n-1)x1, because one column is lost in the diff command.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by