从开始/结束索引列表创建向量化数组

发布于 2024-08-31 16:12:05 字数 357 浏览 5 评论 0原文

我有一个两列矩阵 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

I have a two-column matrix M that contains the start/end indices of a bunch of intervals:

startInd   EndInd
1          3
6          10
12         12
15         16

How can I generate a vector of all the interval indices:

v = [1 2 3 6 7 8 9 10 12 15 16];

I'm doing the above using loops, but I'm wondering if there's a more elegant vectorized solution?

v = [];
for i=1:size(M,1)
    v = [v M(i,1):M(i,2)];
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吻泪 2024-09-07 16:12:05

这是我喜欢用于解决这个特定问题的矢量化解决方案,使用函数 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

Here's a vectorized solution I like to use for this particular problem, using the function 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
橙味迷妹 2024-09-07 16:12:05
cell2mat(arrayfun(@colon,M(:,1)',M(:,2)','UniformOutput',false))

我没有 IMFILL,但在我的机器上,此方法比其他建议更快,并且由于使用 find,我认为会击败 IMFILL 方法。

如果将 M 设置为转置(并且我们调整 arrayfun 的第三个和第四个参数),它可以变得更快。

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).

沧桑㈠ 2024-09-07 16:12:05

可能有一个更好的解决方案,我不知何故没有看到,但这里有一个使用 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

There's probably an even better solution I'm somehow not seeing, but here's a version using 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
执妄 2024-09-07 16:12:05

非常奇怪的解决方案恕我直言,创建临时字符串并使用 EVAL。也可以单线。

tmp = cellstr(strcat(num2str(M(:,1)),{':'},num2str(M(:,2)),{' '}));
v = eval(['[' cell2mat(tmp') ']']);

我知道它可能不适用于大型矩阵。只是为了好玩。

Very weird solution IMHO creating temporary strings and using EVAL. Can be also one-liner.

tmp = cellstr(strcat(num2str(M(:,1)),{':'},num2str(M(:,2)),{' '}));
v = eval(['[' cell2mat(tmp') ']']);

I know it will probably not work on large matrix. Just for fun.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文