在没有循环的矩阵中找到列之间的差异 [英] Finding the difference between columns in matrix without loops
问题描述
我有一个迭代过程的结果,其中每次迭代的结果都彼此相邻放置,如下所示:
I have the results from an iterative process, where the results from each iteration is placed beside eachother, like this:
res =
43.7826 38.8460 38.6889 38.6912 38.6915
107.0735 98.5577 98.1212 98.1170 98.1175
-134.6218 -131.6938 -131.5378 -131.5352 -131.5352
-105.9498 -103.1403 -102.9195 -102.9139 -102.9139
我想做的是创建一个矩阵,该矩阵显示每一列之间的差异,并创建一个矩阵,该矩阵显示从一列到下一列的百分比变化.
What I want to do is create one matrix that shows the difference between each column, and one matrix that shows the percentage change from one column to the next.
使用循环显然很简单,但是有没有循环的聪明方法(也许使用一些内置的Matlab函数)?
This is obviously simple to do using loops, but is there a clever way to do this without loops (maybe using some built-in Matlab functions)?
谢谢.
推荐答案
差异diff(...)命令执行以下操作:
The command diff(...) performes the difference:
diff_res = diff(res,1,2)
在这种情况下,第二维(列)的一阶差. 在您可以计算出百分比之后:
in this case the difference of the first order in the second dimension (columns). After you can compute the percentage:
per_res = diff_res(:,1:4)./res(:,1:4).*100
这篇关于在没有循环的矩阵中找到列之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!