MATLAB:定期提取元素 [英] MATLAB: Extracting elements periodically
本文介绍了MATLAB:定期提取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用:
运算符从向量中提取元素,但要定期进行.例如,说a={1,2,3, ..., 10}
,我想在步骤2中提取元素,更改引用.那我想得到
I'd like to extract elements from a vector using the :
-operator, but periodically. As an example, say a={1,2,3, ..., 10}
and that I would like to extract elements in steps of 2, changing the reference. Then I would like to get
ref 1: 1 3 5 7 9
ref 2: 2 4 6 8 10
ref 3: 3 5 7 9 1
...
MATLAB中是否有关键字强制其为周期性的?还是我必须先将circshift
应用于数组,然后提取?
Is there a keyword in MATLAB to force it to be periodic? Or do I have to apply circshift
to the array first, and then extract?
推荐答案
您可以使用模运算:mod(...-1, numel(a))+1
来构建索引.那些-1
和+1
是必需的,因此生成的索引从1开始(而不是从0开始).
You can build the index using a modulo operation: mod(...-1, numel(a))+1
. Those -1
and +1
are needed so the resulting index is 1-based (not 0-based).
a = [1 2 3 4 5 6 7 8 9 10]; % vector to be indexed
ref = 3; % first value for index
step = 2; % step for index
ind = mod(ref+(0:step:numel(a)-1)-1,numel(a))+1; % build index
result = a(ind); % apply index
这篇关于MATLAB:定期提取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文