Efficient indexing. Index array generating.

10 vues (au cours des 30 derniers jours)
Christian Tieber
Christian Tieber le 2 Fév 2021
data and array1 are given. I am looking for a more efficient way to generate index. It should be a one-liner.
Thanks in Advance!
data = 1:1000;
array1 = 1:10:101
array2 = array+2
index = [];
for i = 1: numel(array1)
index = [index; array1(i):array2(i)];
end
data2 = data(index)

Réponse acceptée

Stephen23
Stephen23 le 2 Fév 2021
data = 1:1000;
array1 = 1:10:101
array1 = 1×11
1 11 21 31 41 51 61 71 81 91 101
idx = array1(:)+(0:2);
out = data(idx)
out = 11×3
1 2 3 11 12 13 21 22 23 31 32 33 41 42 43 51 52 53 61 62 63 71 72 73 81 82 83 91 92 93

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by