Iterate through indexes creating a new matrix

1 回表示 (過去 30 日間)
SRB
SRB 2020 年 4 月 30 日
回答済み: Tommy 2020 年 4 月 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 Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by