Gather data from two matrices depending on values shared in first column
2 views (last 30 days)
Show older comments
I tried using the same method as in the link, but couldn't really figure out a method that worked.
I have two matrices with unique timestamp (in seconds) in the first column. The values are sorted from low to high in both. However, one matrix has more data than the other (about 20000 unique timestamp observations vs 3000).
What i'd like to do is "import" into the smaller matrix the data from the larger matrix that shares the same timestamp.
Can i do this with accumarray again in some way or do i need a loop?
2 Comments
Jan
on 4 Nov 2015
How do you want to include the common data in the smaller array? Do you want to append the data or create a mean between the values, such that the smaller array does not change its size?
Accepted Answer
Jan
on 4 Nov 2015
With appending the found values:
match = ismember(LargerMatrix(:, 1), SmallerMatrix(:, 1));
JoinedMatrix = cat(1, SmallerMatrix, LargerMatrix(match, :));
[dummy, Index] = sort(JoinedMatrix(:, 1));
SortedMatrix = JoinedMatrix(Index, :);
Or creating the mean over the values:
[match, loc] = ismember(LargerMatrix(:, 1), SmallerMatrix(:, 1));
SmallerMatrix(loc, 2:end) = (SmallerMatrix(loc, 2:end) + LargerMatrix(match, 2:end)) * 0.5;
2 Comments
More Answers (0)
See Also
Categories
Find more on Time Series Events in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!