How to remove sets of values from a column vector using two column vectors with the starting and stopping indeces for removal by row
3 次查看(过去 30 天)
显示 更早的评论
Let's say that I have a column vector, x
x = (1:10)'
and two column vectors with the starting and stopping indeces, x_start and x_stop
x_start = [2; 7]
x_stop = [5; 8]
such that I want to remove the values 2->5 and 7->8.
I could use a simple for loop, such as the following:
for i = length(x_start):1
a = x_start(i)
b = x_stop(i)
x(a:b) = []
end
However, I would look to avoid using a for loop as my vector lengths are on the order of 10^5. Is there a more computationally efficient manner to build the indeces that I want to remove from my starting and stopping vectors?
2 个评论
采纳的回答
Fangjun Jiang
2016-8-2
This method uses string. Not sure if it is more efficient.
num=[x_start,x_stop]';
str=sprintf('%d:%d,',num(:));
x(str2num(str))=[];
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!