根据开始值和停止值创建索引向量

时间:2016-09-10 19:45:14

标签: matlab loops vectorization

基于包含索引间隔的开头(第一列)和结束(第二列)的几行的矩阵,我想创建所有索引的向量。例如,如果index = [2 3 4 8 9 10 11 12 13 14 15 16],我希望有以下向量index = []; for n = 1:size(A, 1) index = [index A(n, 1):A(n, 2)]; end

我正在寻找最快的方法。现在,我发现只有两种可能性:

1)带循环

arrayfun

2)index = cell2mat(arrayfun(@(n) A(n, 1):A(n, 2), 1:size(A, 1), 'uni', 0));

arrayfun

有趣的是,{{1}}比循环版本快得多,而且我不知道为什么。另外,我使用从细胞到垫子的转换,这很奇怪。你觉得怎么样?你有其他建议吗?

Thanx求助

2 个答案:

答案 0 :(得分:2)

很难说这有多快,至少没有循环:

A = [1,3;11,13;31,33;41,42;51,54;55,57;71,72];

%// prepare A
A = A.';

%// create index matrix
idx = bsxfun(@plus, A, [0; 1]);
%// special case: 54 and 55 are basically superfluous
%// need to be removed, but 71 and 72 shouldn't
A = A(:); 
dA = diff(A); dA(1:2:end) = 0;
idx = idx(~( [0;dA] == 1 | [dA;0] == 1 ));

%// create mask
mask = zeros(max(A),1);
mask(idx(:)) = (-1).^(0:numel(idx)-1);

%// index vector
out = find(cumsum(mask))
out.' =

      1  2  3 11 12 13 31 32 33 41 42 51 52 53 54 55 56 57 71 72

答案 1 :(得分:2)

以下是一系列方法:

来自https://stackoverflow.com/a/39422485/6579744

方法1

lo = A(:,1);
up=A(:,2);
index=cumsum(accumarray(cumsum([1;up(:)-lo(:)+1]),[lo(:);0]-[0;up(:)]-1)+1);
index= index(1:end-1);

方法2:来自https://stackoverflow.com/a/38507276/6579744。我也提供了相同的答案,但因为Divakar的回答是在我之前他的(修改的)答案首选:

start_idx = A(:,1)';
end_idx = A(:,2)';
lens = end_idx - start_idx + 1;
shift_idx = cumsum(lens(1:end-1))+1;
id_arr = ones(1,sum(lens));
id_arr([1 shift_idx]) = [start_idx(1) start_idx(2:end) - end_idx(1:end-1)];
index = cumsum(id_arr);

方法3:这是我的

N = A(:,2) - A(:,1) +1;
s=cumsum([ 1; N]);
index=(1:s(end)-1) -repelem(s(1:end-1),N) + repelem(A(:,1),N);
%octave    index=(1:s(end)-1) -repelems(s(1:end-1),[1:numel(N);N']) + repelems(A(:,1),[1:numel(N);N']);

方法4 thewaywewalk

中此线程的另一个问题
A = A.';
idx = bsxfun(@plus, A, [0; 1]);
A = A(:); 
dA = diff(A); dA(1:2:end) = 0;
idx = idx(~( [0;dA] == 1 | [dA;0] == 1 ));
mask = zeros(max(A),1);
mask(idx(:)) = (-1).^(0:numel(idx)-1);
index = find(cumsum(mask));

方法5 第二种方法:

index = cell2mat(arrayfun(@(n) A(n, 1):A(n, 2), 1:size(A, 1), 'uni', 0));
来自https://stackoverflow.com/a/39423102/6579744

方法6

sz= size(A, 1);
index_c = cell(1,sz);
for n = 1:sz
    index_c{n} = [A(n, 1):A(n, 2)];
end
index = cell2mat(index_c);

方法7 仅适用于Octave:

idx = 1:size(A ,1);
index_a =bsxfun(@(a,b) (a(b):A (b,2))',A (:,1),idx);
index = index_a(index_a ~= 0); 

方法8 您的第一种方法:

index = [];
for n = 1:size(A, 1)
    index = [index A(n, 1):A(n, 2)];
end

测试数据:

i= 1:500:10000000;
j= i+randi([1 490],1, numel(i));
A = [i', j'];
在Octave中测试

结果,在Matlab中可能会有所不同

method1: 0.077063 seconds
method2: 0.094579 seconds
method3: 0.145004 seconds
method4: 0.180826 seconds
method5: 0.317095 seconds
method6: 0.339425 seconds
method7: 3.242287 seconds
method8: doesn't complete in 15 seconds

bechmark中使用的代码位于Online Demo