Reputation: 57
This post pertains to the "remove elements" operation which utilizes empty brackets, [].
I'm trying to use [] to delete specific elements within a cell. A previous post mentioned that using () rather than {} with [] for an array is the appropriate syntax to delete elements rather than the entire cell. However, this syntax doesn't seem to work (Error: ()-indexing must appear last in an index expression). My original code is as follows, which utilizes {} rather than () to delete the elements contained in each cell of newinter from the corresponding cell of inter2.
for i=1:11
inter2{i}(newinter{i}) = [];
end
inter2 is a 1X11 array. newinter is also a 1x11 array. I used arrays versus matrices because the length of each vector contained within the cells of these arrays is different.
Thanks in advance!
Upvotes: 2
Views: 2795
Reputation: 21563
The solution with intersect is definately the way to go if you want to remove elements using []
, however in general the easiest way to get the elements from one vector that are not in another vector is with setdiff
.
for i=1:11
inter2{i} = setdiff(inter2{i}, newinter{i});
end
I could imagine that this will give slightly better performance if that is a concern.
Upvotes: 0
Reputation: 9864
From your comment seems that newinter
is not containing the indices but the actual values however you are using it for indexing. To remove the elements by value you can use this code instead
for i=1:11
inter2{i}(ismember(inter2{i}, newinter{i}))=[];
end
Upvotes: 2
Reputation: 417
I'm not near a MATLAB pc at the moment, but try:
for i=1:11
inter2{i}(1,newinter{i})=[];
end
Note that if you're trying to remove the contents of the cell rather than the cell itself, you've got it backwards. {}
accesses the contents of the cell, while ()
returns the cell itself, so you should use {}
if you want to keep the cell intact. See this link for an example case.
Upvotes: 0