How Can I Get MATLAB To Parse Data From 2 Arrays?

1 view (last 30 days)
I’ve got 2 arrays that contain timed tagged data. For example, Array A contains 336 lines of data such as this:
NaN 58000.5940000000 -1177.51280000000 5508.35030000000 3007.06600000000
NaN 58002.5620000000 -1178.53030000000 5509.18320000000 3007.87480000000
NaN 58004.4920000000 -1179.45020000000 5511.39770000000 3006.62500000000
NaN 58006.7030000000 -1179.94570000000 5511.93340000000 3006.97660000000
NaN 58008.8950000000 -1181.93950000000 5513.33510000000 3007.40650000000
NaN 58010.7110000000 -1185.03380000000 5515.23320000000 3008.92190000000
NaN 58012.2970000000 -1188.73180000000 5516.97960000000 3010.23010000000
NaN 58014.7150000000 -1191.21170000000 5518.12650000000 3009.92960000000
NaN 58016.8710000000 -1194.66150000000 5519.85710000000 3010.63770000000
NaN 58018.5740000000 -1199.73790000000 5521.98280000000 3012.15330000000
Array B contains 251 lines of data such as the following:
57942.7148437500 2 -5165.91064453000 696.920776367000 5687.62695312500 2.44455700000000e-13
58002.5619062500 2 -5174.50097656000 686.538391113000 5681.06005859400 1.05495600000000e-21
58005.9960937500 2 -5182.81396484000 676.470520019000 5674.66943359400 1.16348400000000e-10
58006.7031875000 2 -5191.15625000000 666.347106933000 5668.22216796900 1.30795700000000e-18
58010.5546875000 3 -5224.20361328000 626.038818359000 5642.33007812500 1.17064600000000e-22
58012.2969750000 3 -5232.68701171000 615.638122558000 5635.59277343800 1.16924600000000e-16
58013.2969750000 3 -5232.68701171000 615.638122558000 5635.59277343800 1.16924600000000e-12
58016.8713562500 3 -5240.89648437000 605.552246093000 5629.03662109400 1.30593000000000e-13
The intent is to parse out, and use, the rows of data in array A (minus the NaNs), and all rows of data in array B – but only when the time tags in array A (column 2) are the same as the time tags in array B (column 1), to 2 decimal places.
For the above arrays, the data associated with time tags 58002.56, 58006.70, 58012.29, and 58016.87 are the targets.
I’ve been reviewing the MATLAB documentation center for reference to a command that would read/match these values, but can’t find one. In the event no such command exists, what would be a sensible approach to parsing out the data of 2 arrays?

Accepted Answer

dpb
dpb on 21 Jun 2013
First round the time columns to 2 digits -- probably easiest way to do this is to just make use of the i/o runtime...
A(:,2)==str2num(num2str(A(:,2),'%.2f')); % round to two decimal places B(:,1)==str2num(num2str(B(:,1),'%.2f')); % ditto
Then just use union to find the indices in A and B that are in common for the two columns
[~,ia,ib]=union(A(:,2),B(:,1)); U=[A(ia,:) B(ib,:)];
  3 Comments
dpb
dpb on 21 Jun 2013
Edited: dpb on 22 Jun 2013
Yeah, you want intersect() not union() -- the two indices in union aren't the same length.
And, while didn't have time before to test, looks like you'll want to convert the timestamps to integers or you'll still have fp comparison problems.
Try
tA=round(A(:,2)*100);
tB=round(B(:,1)*100);
[~,ia,ib]=intersect(tA,tB);
instead. This doesn't overwrite the timestamps in the two original arrays; you may want to for convenience of having the rounded version after you're satisfied logic is ok
Then you can obviously select the desired columns of A,B and order them as desired eliminating the duplicates and NaN in the process.
IOW, "salt to suit"... :)
Brad
Brad on 24 Jun 2013
Intersect!!! I completely forgot about this command. Thanks DPB.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!