Using index to match rows of table

조회 수: 5 (최근 30일)
Lukas Netzer
Lukas Netzer 2021년 8월 9일
편집: Lukas Netzer 2021년 8월 10일
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!

답변 (1개)

Peter Perkins
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.
  댓글 수: 1
Lukas Netzer
Lukas Netzer 2021년 8월 10일
편집: Lukas Netzer 2021년 8월 10일
Hey Peter,
thanks for your answer. I will give you more context, which should describe my problem better:
I have two tables. Table1 contains:
table1.WP1 = {0;0;145;169;1693;1708;2729;0;0;48;1382;1390;1896;1899;2549;2569;0;0;145;169;1693;1708;2638;0};
table1.WP2 = {0;145;169;1693;1708;2729;2779;0;48;1382;1390;1896;1899;2549;2569;2750;0;145;169;1693;1708;2638;2737;0};
table1.WC = {0;0;1;1;1;1;0;0;0;1;1;1;1;1;1;0;0;0;1;1;1;1;0;0};
Whereas table2 contains the following:
table2.WP1 = {0;0;5;115;219;262;328;408;424;531;562;620;676;781;805;808;919;1009;1112;1183;1241;1254;1283;1323;1346;1362;1383;1472;1548;1559;1578;1600;1642;1693;1700;1763;1843;1861;1966;1982;2135;2150;2159;2197;2233;2253;2301;2358;2377;2394;2419;2438;2453;2486;2506;2515;2528;2554;2559;2577;2637;2694;2716;2737;2764;2777;2779;0;0;2;15;41;48;66;104;174;197;205;241;260;282;309;318;339;363;379;400;436;465;512;519;546;576;602;613;787;870;897;968;1031;1082;1147;1165;1169;1181;1250;1339;1382;1404;1442;1467;1532;1607;1668;1711;1791;1874;1896;1910;1942;1959;2025;2085;2138;2275;2313;2379;2451;2491;2549;2597;2621;2658;2687;2723;2729;2731;2735;2739;2745;2746;2750;0;0;5};
table2.WP2 = {0;5;115;219;262;328;408;424;531;562;620;676;781;805;808;919;1009;1112;1183;1241;1254;1283;1323;1346;1362;1383;1472;1548;1559;1578;1600;1642;1693;1700;1763;1843;1861;1966;1982;2135;2150;2159;2197;2233;2253;2301;2358;2377;2394;2419;2438;2453;2486;2506;2515;2528;2554;2559;2577;2637;2694;2716;2737;2764;2777;2779;2779;0;2;15;41;48;66;104;174;197;205;241;260;282;309;318;339;363;379;400;436;465;512;519;546;576;602;613;787;870;897;968;1031;1082;1147;1165;1169;1181;1250;1339;1382;1404;1442;1467;1532;1607;1668;1711;1791;1874;1896;1910;1942;1959;2025;2085;2138;2275;2313;2379;2451;2491;2549;2597;2621;2658;2687;2723;2729;2731;2735;2739;2745;2746;2750;2750;0;5;115};
table2.distance = {0;114.600000000000;3914.20000000000;7696.20000000000;4513.30000000000;7454.20000000000;8289.70000000000;1003.40000000000;11139;2183.60000000000;6023.80000000000;6680.40000000000;11065.1000000000;800.700000000000;49.6000000000000;6109.90000000000;6136;8375.60000000000;4463.50000000000;5994.60000000000;1155.30000000000;1879.70000000000;4298.50000000000;2795.80000000000;1225.50000000000};
Out of these I made an Index both for table 1 and 2, where whenever WP1 and WP2 are 0, we add a STOP:
B=table1.WP1==0 & table1.WP2==0;
IndStopt1=find(B);
Bwp=table2.WP1 & table2.WP2==0;
IndStopt2=find(Bwp);
These Indices have the same length --> same amount of Stops, that's good and how it should be :)
IndStopt1 = [1;8;17;24;26;28;35;45;53;57;68;75;79;81;85;96;103;108;118;125;130;136;146;152;158];
IndStopt2 = [1;68;143;213;225;248;293;333;397;426;474;541;561;567;606;654;721;750;802;830;859;899;938;962;994];
The Indices are used to define the rows of each table that need to be matched inbetween stops.
e.g. --> rows 2 to 7 of table1 need to be matched with rows 2 to 67 of table 2. Stops are left out.
Now I want to get table2.WC, which should be alligned with table1.WC and should look like that:
table2.WC = {0;0;0;x;1;1;1;1;...}
Notice x --> table1.WP1/WP2/WC = (0,145,0),(145,169, 1), (169,1693, 1)
But table2.WP1/WP2 = (115, 219) --> partly fits both of them. That's a special case I need to adress (later?) and give it values according to its "edges". Here we have 30 edges (145-115) in 0 and 74 (219-145) edges in 1. This needs to be handled accordingly but am not sure as of right now how to do it.
table2.WC will later be used to run against table2.distance, where distance will only be used if table2.WC is 1.
So my main problem as of right now is matching the rows according the specidifed Indices and the next problem will be how to deal with the special cases.
What I've tried so far, but does not seem to work:
for n = 1:length(IndStopt1) - 1
for k = IndStopt2((n)+1):IndStopt2((n+1)-1)
for l = IndStopt1((n+1)):IndStopt1((n+1)-1)
if table2.WP1(k) >= table1.WP1(l) &&...
table2.WP2(k) <= table1.WP2(l)
table2.WC(k) = table1.WC(l)
end
end
end
end
Thanks for reading - again, any hint as to what approach is the best to take is very much appreciated!
Note: presented tables and columns don't match but should give a clue as to what I am dealing with!
BRG, Lukas

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by