Selection by circular indexing
2 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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 Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!