【问题标题】:Sending a Data Range to Another Data Range in Matlab without a for loop在没有 for 循环的情况下在 Matlab 中将数据范围发送到另一个数据范围
【发布时间】:2013-12-21 00:17:43
【问题描述】:

这就是我所拥有的:

index : [3;4;5;6;7;8;9] 我有一个对应于该索引的向量:

inVector = [10;20;30;40;50;60;70;80;90]

我想要的是获得 invector 中每 3 行的平均值。因此,

outVector = mean(inVector[index - 2 : index]);

因此,outVector 现在有 [20;30;40;50;60;70;80];

【问题讨论】:

  • index有什么用?
  • 这只是我遍历的一个索引,然后通过 inVector 中相应的前 3 行来获取平均值。
  • 如果索引总是x:end,你可以查看filter

标签: matlab loops indexing


【解决方案1】:

对于任意index,您可以使用cumsum

N = 3;
aux = cumsum([0; inVector(:)]);
result = (aux(index+1)-aux(index-N+1))/N;

如果index 总是由连续的索引组成,那么使用conv 会更容易(因为移动平均线是带有矩形窗口的卷积):

N = 3;
result = conv(inVector,ones(1,N)/N,'valid');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    相关资源
    最近更新 更多