发现在不同长度的阵列相同的日期时间 [英] find same datetime in arrays of different lengths
问题描述
考虑朱利安日期以下阵列
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.
如何将有可能找到其日期是在3阵列,即返回所有阵列之间的一致的日期时间的指数相同?
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?
我知道,如果他们是相同的大小,我可以使用的strcmp,但你会怎么做,如果他们是不同的长度作为例子吗?此外,STRCMP这里会是一个问题,因为数组是数3而不是2。
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.
推荐答案
使用相交
:
common_dates = intersect(intersect(Jday1, Jday2), Jday3);
如果您还需要指标(基于@Robert P.下面留言,请注意相交内的位置的变化
)
[common_dates, idx] = intersect(Jday1, intersect(Jday2, Jday3));
更新
如果您有多个阵列相交,写一个小函数来处理这项工作:
If you have multiple arrays to intersect, write a small function to handle the job:
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
这篇关于发现在不同长度的阵列相同的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!