删除数组中特定索引范围之间的值 [英] Delete values between specific ranges of indices in an array

查看:299
本文介绍了删除数组中特定索引范围之间的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:

Z = [1 24 3 4 52 66 77 8 21 100 101 120 155];

我还有另一个数组:

deletevaluesatindex=[1 3; 6 7;10 12]

我想删除数组deletevaluesatindex中表示的索引(1到3、6到7、10到12)的数组Z中的值

I want to delete the values in array Z at indices (1 to 3, 6 to 7, 10 to 12) represented in the array deletevaluesatindex

所以Z的结果是:

Z=[4 52 8 21 155];

我尝试使用下面的表达式,但它不起作用:

I tried to use the expression below, but it does not work:

X([deletevaluesatindex])=[]

推荐答案

这可以做到:

rdvi= size(deletevaluesatindex,1);   %finding rows of 'deletevaluesatindex'
temp = cell(1,rdvi);     %Pre-allocation

for i=1:rdvi
    %making a cell array of elements to be removed 
    temp(i)={deletevaluesatindex(i,1):deletevaluesatindex(i,2)};
end

temp = cell2mat(temp); %Now temp array contains the elements to be removed
Z(temp)=[]  % Removing the elements

这篇关于删除数组中特定索引范围之间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆