Finding value pairs in subsequent arrays
16 次查看(过去 30 天)
显示 更早的评论
I am working with options data and am looking for Call-Put Pairs in a huge data set. I am fairly new to MatLab and in desparate need of help.
For example, I have a table with 4 columns and 11 arays, which looks like this:
What i need is to remove all arrays, which do not have matching Call "C" or vice versa a matching Put "P" to them, with same "date" and "exdate" and "strike_price".
So in the end, i would only need array 10&11, as they match in column 1/2/4 and do not match in cloumn 3.
Can anyone help me? Thanks in advance and all the best.
0 个评论
采纳的回答
Cam Salzberger
2021-9-13
Hello Kai,
If the order doesn't matter (i.e. the "call" doesn't need to be before the "put"), you could separate out the data into two tables, one for call entries and one for put entries. It might look something like:
whichCall = strcmp(dataTable.cp_flag, "C");
callTable = dataTable(whichCall, :);
putTable = dataTable(~whichCall, :);
Then you can go through one of them row-by-row (probably best if it's whichever table is usually shorter), and see if there is a matching entry in the other table. If there's a matching entry, keep the one in the table you are iterating. Otherwise, remove that line. I'll actually track it individually and remove it all at the end, for better indexing.
nCalls = size(callTable, 1);
whichCallRowsKeep = false(nCalls, 1);
for k = 1:nCalls
% Use element-wise AND (&) to compare each row together
whichMatchAllThree = ...
putTable.date == callTable.date(k) & ...
putTable.exdate == callTable.exdate(k) & ...
putTable.strike_price == callTable.strike_price(k);
% If there are any matching put rows, keep the call row
whichCallRowsKeep(k) = any(whichMatchAllThree);
end
% Remove all call rows with no matching put entry
filteredCallTable = callTable(whichCallRowsKeep, :);
-Cam
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!