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!