How do I implement this for loop calculation in direct matrix or vector calculation?

1 次查看(过去 30 天)
I have a matrix A of order (n+1)*3. And I have to perform a calculation. for loop implementtaion of it is given below.
R=0.0;
for j=1:1:n
R=R+(A(j+1,1)-A(j,1))^2+(A(j+1,2)-A(j,2))^2+(A(j+1,3)-A(j,3))^2;
end
How do I implement above calculation using matrix or vector notation. I need this for making optimization code run faster.
Thank you.

采纳的回答

David Hill
David Hill 2021-10-2
R=sum(diff(A).^2,'all');

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by