2

Consider the following arrays of julian dates

Jday1 = datenum('2011-01-01 00:00','yyyy-mm-dd HH:MM'):60/(60*24):...
    datenum('2011-12-31 23:00','yyyy-mm-dd HH:MM');
Jday2 = datenum('2011-04-01 00:00','yyyy-mm-dd HH:MM'):60/(60*24):...
    datenum('2011-12-31 23:00','yyyy-mm-dd HH:MM');
Jday3 = datenum('2011-02-06 00:00','yyyy-mm-dd HH:MM'):60/(60*24):...
    datenum('2011-12-31 22:00','yyyy-mm-dd HH:MM');

which are all of different length and have different time within them.

How would it be possible to find which dates are the same in the 3 arrays i.e. return the index of the consistent dateTime between all of the arrays?

I know I could use strcmp if they were the same size but what would I do if they are of different lengths as in the example? Also, strcmp would be an issue here due to the number of arrays being 3 not 2.

4

2 回答 2

4

使用intersect

common_dates = intersect(intersect(Jday1, Jday2), Jday3);

或者如果您还需要索引(根据@Robert P. 下面的评论,请注意 inner 位置的变化intersect):

[common_dates, idx] = intersect(Jday1, intersect(Jday2, Jday3));

更新

如果您有多个要相交的数组,请编写一个小函数来处理这项工作:

function varargout = intersectn(varargin)
narginchk(2, Inf);
nargoutchk(0, 2);
x = varargin{nargin}; 
for i = nargin-1:-1:1
    [x, idx] = intersect(varargin{i}, x); 
end
if nargout > 1, varargout{2} = idx; end
if nargout > 0, varargout{1} = x; else disp(x); end
于 2013-06-12T18:50:01.400 回答
0

由于您将数组存储为 datenums 而不是字符串,因此您可以将它们与Compare = (Jday1 == Jday2)等等进行比较。这仅在两个向量长度相同时才有效。

如果向量的长度不同,您可以使用在 set 中可以找到的每个索引处Compare = ismember(Jday1, Jday2)返回一个大小Jday1与 1 (true)相同的数组。iJday1(i)Jday2

关于 ismember 的文档可以在这里找到

最后,如果您只想返回一个列表等于另一个列表的索引,请尝试find(ismember(Jday1, Jday2)).

于 2013-06-12T18:47:13.103 回答