I have a vector [1 2 3 4 ... 100] and another vector storing the numbers I want to delete like [2 3 4]. After deleting, the numbers in original vector should be mapped to another order, for example, 1->1, 5->2, 6->3, etc. Is there any efficient way to do this? Thanks a lot!
Asked
Active
Viewed 76 times
2 Answers
1
I'd use setdiff
:
% original vector
A = 1:100;
% elements to be removed
B = [2 3 4 18 21];
% new order (indices)
C = randperm(numel(A)-numel(B));
% Step 1) remove the elements
[D,I] = setdiff(A,B); % ordered
D = A(I); % restore original order
% Step 2) re-order the elements
D = D(C)

Rody Oldenhuis
- 37,726
- 7
- 50
- 96
-
The new order is not correct...What's the purpose of using C = randperm(numel(A)-numel(B))? – Jin Yan Jun 04 '13 at 13:45
0
You can do:
original_vector = 1:100;
delete_vector = [2 3 4];
for ii = 1:length(delete_vector)
original_vector(original_vector==delete_vector(ii)) = [];
end

Rody Oldenhuis
- 37,726
- 7
- 50
- 96

fatihk
- 7,789
- 1
- 26
- 48
-
Small edit: the use of `find` is unnecessary. Moreover, [it's not recommended to use `i` and `j` as loop variables](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab). – Rody Oldenhuis Jun 04 '13 at 06:01
-
this only removes the elements, but doesn't re-order them as the OP asked; it is only a partial answer... – Rody Oldenhuis Jun 04 '13 at 08:00