필터 지우기
필터 지우기

Selection by circular indexing

조회 수: 1 (최근 30일)
mahdi Babayi semiromi
mahdi Babayi semiromi 2021년 7월 1일
댓글: mahdi Babayi semiromi 2021년 7월 1일
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