Using index to match rows of table
조회 수: 5 (최근 30일)
이전 댓글 표시
I have an Index:
index = [1;3;7;10;15;17;20];
I want to use that index to access rows in table 1:
table1.WP1 = [1;8;17;24;26;28;35;45;53;57;68;75;79;81;85;96;103;108;118;125];
table1.WP2 = [8;17;24;26;28;35;45;53;57;68;;75;79;81;85;96;103;108;118;125;130];
I always need to access a range of rows, defined by:
k = 1:height(index)-1
range = index(k)+1:index(k+1)-1
which should result in the rows per iteration that need to be checked:
range = [2:2;4:6;8:9;11:14;16:16;18:19]
So in iteration 1 I would want to access rows 2:2 which should give me the values:
[8,17]
Iteration 2 accesses rows 4:6 and therefor checks values:
[[24,26],[26,28],[28:35]]
I hope this is somewhat understandable. Any help or hints are appreciated, as to what technique might be the best for that!
Thank you!
댓글 수: 0
답변 (1개)
Peter Perkins
2021년 8월 9일
Lukas, you don't say what you actually need to do after getting those rows, but I'm going to suggest that you should be using rowfun with a grouping variable. Here's the grouping variable:
>> g = zeros(20,1); g(index) = 1;
>> cumsum(g)
ans =
1
1
2
2
2
2
3
3
3
4
4
4
4
4
5
5
6
6
6
7
Add that to your table, and you are all set. No looping.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!