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 CenterFile Exchange 中查找有关 Debugging and Analysis 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by