在向量子集上进行MATLAB计算,然后移位并重复 [英] MATLAB calculation on subset of vector, then shift, and repeat

查看:117
本文介绍了在向量子集上进行MATLAB计算,然后移位并重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要取向量的一个子集的最大值,然后在向量中移动.例如列向量

I need to take the max of a subset of a vector and then shift through the vector. For instance the column vector

a=[1;2;3;4;5;6;7]

如何获取max(a(1:3))max(a(2:4)),...,max(a(5:end))并将所有输出放置在另一个向量中?我可以轻松地通过for循环执行此操作,但是我正在寻找一种使用MATLAB中的矩阵运算(最好是在一行代码中)进行操作的优雅方式(即使我意识到MATLAB中的矩阵运算可能会这样做)正在使用for循环来实现).

how can I take max(a(1:3)), max(a(2:4)), ..., max(a(5:end)) and place all the output in another vector? I can easily do this with a for loop however I'm looking for an elegant way of doing so using matrix operations in MATLAB and preferably in a single line of code (even though I realize that the matrix operations in MATLAB to do so may likely be using a for loop to implement).

谢谢!

推荐答案

假设您的移位和窗口长度是恒定的(分别为1和3),则可以使用

Assuming that your shift and window length is constant (1 and 3 in your case resp.), you can use nlfilter to define a general sliding-window operations on a vector:

a = [1:7];

fun = @(x) max(x(:));
b = nlfilter(a, [1 4], fun);

这篇关于在向量子集上进行MATLAB计算,然后移位并重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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