Borrar filtros
Borrar filtros

How to find prices that occur on the same dates for two sets of stock index prices

2 visualizaciones (últimos 30 días)
I have two sets of stock index returns, both in large 2000ish x 2 column arrays, with dates in the left hand column, and prices in the right. The shorter array only contains 2844 days of data, whereas the longer array contains 2898 days of data.
I am trying to get rid of all days of data that do not occur in both arrays so that I have two equally sized arrays that I can work with ( for example if one array has price data for the 10th of january 2020, but the other does not, I would want to remove that cell).
I am pretty stuck so would really appreciate some help!
Thanks
% An example of the process I would like to achieve is as follows:
shorterarray = [60,70,80 ; '1/1/2020','2/1/2020','4/1/2020']
longerarray = [60,65,75,80 ; '1/1/2020','2/1/2020','3/1/2020','4/1/2020' ]
% since the date 3/1/2020 is only included in the longer array, I would
% want to remove that date from the longer array, along with its
% corresponding price, 75.
% The larger array has the majority of the values that I would like to
% remove because they are not in the smaller array, but my main attempt
% seems to keep getting tripped up by the fact that there are some date
% values in the smaller array that are not in the larger array at all.
while length(testnikkei)~=length(testnasdaq)
for i=1:2844
% this logical part of the for loop works because I have converted
% the dates into datenum format
if testnikkei(i,1)==testnasdaq(i,1)
disp(i);
elseif testnikkei(i,1)~=testnasdaq(i,1)
% trying to delete cells from the larger array when they are
% not equal
testnasdaq(i,:)=[];
break
end
end
end
% testnikkei is the shorter array (size 2844), testnasdaq is the longer
% array (size 2898)

Respuesta aceptada

Voss
Voss el 23 de Mzo. de 2022
shorterarray = {'1/1/2020' 60; '2/1/2020' 70; '4/1/2020' 80}
shorterarray = 3×2 cell array
{'1/1/2020'} {[60]} {'2/1/2020'} {[70]} {'4/1/2020'} {[80]}
longerarray = {'1/1/2020' 60; '2/1/2020' 65; '3/1/2020' 75; '4/1/2020' 80}
longerarray = 4×2 cell array
{'1/1/2020'} {[60]} {'2/1/2020'} {[65]} {'3/1/2020'} {[75]} {'4/1/2020'} {[80]}
% remove rows from longerarray whose first-column value is not in the first
% column of shorterarray:
longerarray(~ismember(longerarray(:,1),shorterarray(:,1)),:) = []
longerarray = 3×2 cell array
{'1/1/2020'} {[60]} {'2/1/2020'} {[65]} {'4/1/2020'} {[80]}

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by