Using index to match rows of table
2 次查看(过去 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!