views:

117

answers:

3

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
+1  A: 

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
Jonas
A: 

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.

yuk
+3  A: 

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
gnovice
Yes, I knew there is an easy solution. Very elegant. +1
yuk
I suspected as much. :) +1
Jonas
exactly what I was looking for.. Thanks
merv

related questions