Gather data from two matrices depending on values shared in first column

2 views (last 30 days)
This is kinda a follow-up to this question.
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
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?
Rasmus Larsen
Rasmus Larsen on 4 Nov 2015
Edited: Rasmus Larsen on 4 Nov 2015
Yes i want to append the data so the smaller array doesn't change size. But not a mean between the values.
The smaller array has data for the transaction price of a stock. The larger array has bid/ask data for this stock. But i don't need the bid/ask data if there wasn't a transaction in that second (e.g. no transaction price for that timestamp in the smaller array).
Does that makes sense?
Edit: Oh didn't see your answer. I'll try it out

Sign in to comment.

Accepted Answer

Jan
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
Rasmus Larsen
Rasmus Larsen on 4 Nov 2015
Edited: Rasmus Larsen on 4 Nov 2015
Okay so it kinda works.
Thing is there's now 2 observations for each timestamp, so the SortedMatrix is twice the size of the original smaller matrix (2972 vs 5944 observations).
I think i might have explained it wrong. I'd like to append the values from the larger matrix to each row instead of as a new row. So the final SortedMatrix would also be 2972 observations (rows), but would have the data from that particular timestamp added as new columns for each row.
Rasmus Larsen
Rasmus Larsen on 4 Nov 2015
Nevermind i solved that by changing the cat dimension to 2.
Thanks a lot for your help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!