1

I have an array :

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

I have another array:

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

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

So the result of Z is:

Z=[4 52 8 21 155];

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

X([deletevaluesatindex])=[]
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
ryh12
  • 357
  • 7
  • 18

4 Answers4

1

Another solution using bsxfun and cumsum:

%// create index matrix
idx = bsxfun(@plus , deletevaluesatindex.', [0; 1])
%// create mask
mask = zeros(numel(Z),1);
mask(idx(:)) = (-1).^(0:numel(idx)-1)
%// extract unmasked elements
out = Z(~cumsum(mask))

out =    4    52     8    21   155
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
0

If you control how deletevaluesatindex is generated, you can instead directly generate the ranges using MATLAB's colon operator and concatenate them together using

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

then use the expression you suggested

Z([deletevaluesatindex])=[]

If you have to use deletevaluesatindex as it is given, you can generate the concatenated range using a loop or something like this

lo = deletevaluseatindex(:,1)
up = deletevaluseatindex(:,2)
x = cumsum(accumarray(cumsum([1;up(:)-lo(:)+1]),[lo(:);0]-[0;up(:)]-1)+1);
deleteat = x(1:end-1)
logan rakai
  • 2,519
  • 2
  • 15
  • 24
  • I have control on how deletevaluesatindex is generated. if I have x=1,y=3, how can I concatenate them to become 1:3 and insert then to deletevaluesatindex. I cann't concatenate them as string, then I cann't use then in Z([deletevaluesatindex])=[] – ryh12 Sep 10 '16 at 05:03
  • `x:y` will generate `1 2 3`, so you can do `deletevaluesatindex = horzcat(deletevaluesatindex, x:y)` – logan rakai Sep 10 '16 at 05:29
0

Edit: as in comments noted this solution only works in GNU Octave

with bsxfun this is possible:

Z=[1 24 3 4 52 66 77 8 21 100 101 120 155];
deletevaluesatindex = [1 3; 6 7;10 12];
idx = 1:size(deletevaluesatindex ,1);
idx_rm=bsxfun(@(A,B) (A(B):deletevaluesatindex (B,2))',deletevaluesatindex (:,1),idx);
Z(idx_rm(idx_rm ~= 0))=[]
rahnema1
  • 15,264
  • 3
  • 15
  • 27
  • it is giving the following error: Error using bsxfun Invalid output dimensions. Error on line idx_rm=bsxfun(@(A,B) (A(B):deletevaluesatindex (B,2))',deletevaluesatindex (:,1),idx); – ryh12 Sep 10 '16 at 05:47
  • @ryh12 I tested in GNU Octave. online demo: http://rextester.com/OCUL14882 – rahnema1 Sep 10 '16 at 06:51
  • @Sardar_Usama Thanks,answer updated, mentioned that it only works in Octave – rahnema1 Sep 10 '16 at 16:58
0

This will do it:

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
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58