Iterate through indexes creating a new matrix

1 Ansicht (letzte 30 Tage)
SRB
SRB am 30 Apr. 2020
Beantwortet: Tommy am 30 Apr. 2020
I have a 169x1 array containing start indexes (array A). I have a 169x1 array containing stop indexes (array B). I have a 22394x2 matrix containing data (matrix C). I want to create a new matrix (matrix D) containg only data from Matrix C that is between the start and stop indexes from Matrix A and B (including the start and stop locations). For example, C(A(1,1):B(1,1),1) would be the first range of numbers in matrix D, C(A(2,1):B(2,1),1) would be the second range of numbers in matrix D, C(A(3,1):B(3,1),1) would be the third range of numbers in matrix D, etc. Any help is much appreciated. Thanks!

Akzeptierte Antwort

Tommy
Tommy am 30 Apr. 2020
Try this:
i = 1:size(C,1);
D = C(any(i>=A & i<=B),1);
Works with the following simple arrays, but let me know if it doesn't work for you.
C = randi(10,20,2);
A = [2;5;10];
B = [3;8;15];
i = 1:size(C,1);
D = C(any(i>=A & i<=B),1);
Results:
>> C
C =
7 6
10 8
3 6
10 5
4 10
6 2
7 2
5 7
2 9
7 4
8 5
6 3
7 3
4 6
6 9
9 5
10 6
3 7
6 8
5 6
>> D
D =
10
3
4
6
7
5
7
8
6
7
4
6

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by