Iterate through indexes creating a new matrix

1 次查看(过去 30 天)
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!

采纳的回答

Tommy
Tommy 2020-4-30
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by