M
我有一个包含一堆间隔的开始/结束索引的两列矩阵:
startInd EndInd
1 3
6 10
12 12
15 16
如何生成所有区间索引的向量:
v = [1 2 3 6 7 8 9 10 12 15 16];
我正在使用循环执行上述操作,但我想知道是否有更优雅的矢量化解决方案?
v = [];
for i=1:size(M,1)
v = [v M(i,1):M(i,2)];
end
M
我有一个包含一堆间隔的开始/结束索引的两列矩阵:
startInd EndInd
1 3
6 10
12 12
15 16
如何生成所有区间索引的向量:
v = [1 2 3 6 7 8 9 10 12 15 16];
我正在使用循环执行上述操作,但我想知道是否有更优雅的矢量化解决方案?
v = [];
for i=1:size(M,1)
v = [v M(i,1):M(i,2)];
end
这是我喜欢用于这个特定问题的矢量化解决方案,使用函数cumsum
:
v = zeros(1, max(endInd)+1); % An array of zeroes
v(startInd) = 1; % Place 1 at the starts of the intervals
v(endInd+1) = v(endInd+1)-1; % Add -1 one index after the ends of the intervals
v = find(cumsum(v)); % Perform a cumulative sum and find the nonzero entries
cell2mat(arrayfun(@colon,M(:,1)',M(:,2)','UniformOutput',false))
I don't have IMFILL, but on my machine this method is faster than the other suggestions and I think would beat the IMFILL method due to the use of find.
It can be made even faster if M is set up transposed (and we adjust the third and fourth arguments of arrayfun).
我可能没有看到更好的解决方案,但这里有一个使用IMFILL的版本
startInd = [1,6,12,15];
endInd = [3,10,12,16];
%# create a logical vector with starts and ends set to true to prepare for imfill
tf = false(endInd(end),1);
tf([startInd,endInd]) = true;
%# fill at startInd+1 wherever startInd is not equal endInd
tf = imfill(tf,startInd(startInd~=endInd)'+1); %' SO formatting
%# use find to get the indices
v = find(tf)' %' SO formatting
v =
1 2 3 6 7 8 9 10 12 15 16
非常奇怪的解决方案恕我直言,创建临时字符串并使用 EVAL。也可以单线。
tmp = cellstr(strcat(num2str(M(:,1)),{':'},num2str(M(:,2)),{' '}));
v = eval(['[' cell2mat(tmp') ']']);
我知道它可能不适用于大型矩阵。纯娱乐。