Reputation: 4364
I have this matrix of size 10000 by 1. I want to remove some rows lets say 50 from random position. After removing the rows the size of the matrix will be 9950. So, how can I get the index of any element from the original 10000. I mean suppose I want to get the value of the 9999th element as index in the original matrix. But now since the size of my new matrix is 9950 after removing some rows, how can I know which is the 9999th element from the original matrix. What is the best way to do this?
Upvotes: 0
Views: 354
Reputation: 3038
So you want to shrink the matrix randomly, but still remember where each element came from? And know where the original elements ended up? One way to do this would be to keep an index that remembers where the elements in the shrunken matrix came from.
Let's say your original matrix is
A = rand(10000,1);
The index that remembers where these elements come from is:
A_idx = [1:length(A)]';
Now, borrowing from @yuk, let's randomly remove some elements:
rp_idx = randperm(numel(A));
kill = rp_idx(1:50);
A(kill) = [];
A_idx(kill) = [];
Now however many times you follow this procedure and remove elements, the original location of A(i)
was A_idx(i)
.
And the kth element of the original matrix can now be found at
new_idx = find( A_idx == k )
in the new shrunken matrix. Note that new_idx
will be empty if the element you're looking for is one of the ones that got removed.
Upvotes: 2
Reputation: 19880
Usually once you have index of elements to remove you remove them all at the same time. For example, let's generate random index of 50 elements out of 10000 vector A
.
rp_idx = randperm(numel(A));
rp_idx_50 = rp(1:50);
Then we can remove those 50 elements as
A(rp_idx_50) = [];
If you for some reason have to remove the elements one-by-one, sort the index and start from the end. In this case you don't have to find the new index.
Upvotes: 0