Matlab减去矩阵元素 [英] Matlab subtracting matrix elements

查看:210
本文介绍了Matlab减去矩阵元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个矩阵

数据=

 1
 3
 4
 3
 5
 2
 5

我需要通过减去元素来获取新数据 像这样

i need to get new data by subtracting the element like this

data2-data1
data3-data2
data4-data3
data5-data4
data6-data5
data7-data4
...
datan-data(n-1)

因此从该数据中输出应为 im =

so from that data the output should be im =

 2
 1
-1
 2
-3
 3

我仍在尝试操作此代码,但出现错误

i still trying to manipulate this code but got an error

clc
data=[1;3;4;3;5;2;5]
cnt=size(data,1)

for i=1:cnt;
    im=(data(i)-(data(i-1)));
end
im

推荐答案

diff可以满足您的要求.

diff(data)

但是,如果您想继续使用该方法,我想您无法在i = 1时定义与data(i-1)相关的错误.您的循环应为2:cnt.

BUT if you want to continue with your approach, I imagine the error you get is related to data(i-1) cannot be defined when i = 1. Your loop should be 2:cnt.

另一种方法是data(2:end) - data(1:end-1)

这篇关于Matlab减去矩阵元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆