Selection by circular indexing

2 次查看(过去 30 天)
Hi
I have a vector :
v = (1:10)';
I want to have a function that can select a segment of the vector from index "a" to index "b" such that if "a" is greater than "b", it loops back on the vector and starts from the beginning, i.e., I want the function
function y = circularSelect(v , a, b)
%%
end
such that
circularSelect(v , 7 , 2)
returns
[7, 8 ,9 ,10 , 1 , 2]
I'd like to know if there's a way to do it without using "if" statements, since it's quite trivial how to do it with an "if" statement.
thanks for your answers in advance

采纳的回答

Yazan
Yazan 2021-7-1
function y = circularSelect(v, a, b)
N = length(v);
idx = a:b+N*(b<a);
idx(idx>N) = idx(idx>N)-N;
y = v(idx);
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by