Selection by circular indexing

2 ビュー (過去 30 日間)
mahdi Babayi semiromi
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
  1 件のコメント
mahdi Babayi semiromi
mahdi Babayi semiromi 2021 年 7 月 1 日
Thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by