Borrar filtros
Borrar filtros

Selection by circular indexing

1 visualización (últimos 30 días)
mahdi Babayi semiromi
mahdi Babayi semiromi el 1 de Jul. de 2021
Comentada: mahdi Babayi semiromi el 1 de Jul. de 2021
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

Respuesta aceptada

Yazan
Yazan el 1 de Jul. de 2021
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

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by