我总是被告知在 MATLAB 中几乎所有的 for 循环都可以省略,而且它们通常会减慢进程。那么这里有办法吗?:
我有一个元胞数组 (tsCell
)。 tsCell
存储不同长度的时间数组。我想为所有时间数组找到一个相交时间数组 (InterSection
):
InterSection = tsCell{1}.time
for i = 2:length{tsCell};
InterSection = intersect(InterSection,tsCell{i}.time);
end
最佳答案
这是另一种方式。这还假设每个原始向量中没有重复项。
tsCell_time = {[1 6 4 5] [4 7 1] [1 4 3] [4 3 1 7]}; %// example data (from Divakar)
t = [tsCell_time{:}]; %// concat into a single vector
u = unique(t); %// get unique elements
ind = sum(bsxfun(@eq, t(:), u), 1)==numel(tsCell_time); %// indices of unique elements
%// that appear maximum number of times
result = u(ind); %// output those elements
关于arrays - MATLAB中没有for循环的多个数组的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35825435/