Split Vector based on indices from another vector
5 次查看(过去 30 天)
显示 更早的评论
I have a vecotr let's say x=1:200 which is 1x200. Now i have another vector let's say y that is for example 10x1 holds the points where I should split on using predefined range. For example, if I want 5 points before y and 10 points after y, so if I have y=[20;50;120], then the output should be like this [15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
[45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60]
[115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130]
I tried to set y1=y-5, and y2=y+10, then applied
A=x(1,y1:y2)
but always the first subarray is in the output. Is there any way to divide my array without a for loop because the original array is about 500,000 in length. Thanks in advance
0 个评论
采纳的回答
更多回答(1 个)
Andrei Bobrov
2017-4-4
编辑:Andrei Bobrov
2017-4-4
x = 1:200;
y=[20;50;120];
dy = 16;
z = zeros(size(x));
z(y - 5) = 1;
z(y + 10) = -1;
z1 = cumsum(z);
out = reshape(x(z1 ~= 0),dy,[])';
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!