0

I have array contain 1 column with 225 rows and I want to select 170 elements from these elements randomly and store it in another array also store the remain elements at another array, I used this code to choose randomly elements but I don't know how I can store the remain elements (55) at another array !

Code : my original array A

msize = numel(A);
firstpart = A(randperm(msize, 170))

secondpart = !!!!! ( remain elements ) % This is my question 

2 Answers 2

2

Instead of throwing away the other elements, just get a permutation of all of them and then partition the array:

msize = numel(A);
allperm = A(randperm(msize));
firstpart = allperm(1:170);
secondpart = allperm(171:end);
Sign up to request clarification or add additional context in comments.

Comments

2

You can use boolean indexing.

A = rand(255,1); % just generating an example matrix
indices = false(size(A));
indices(randsample(1:numel(A),170)) = true; % select what to keep
firstpart = A(indices);
secondpart = A(~indices);

3 Comments

I like this idea, particularly if it's important to keep the elements in sorted order.
Haha, I liked (+1) your idea, it's very elegant : ) I've had situations before where having boolean indexing is useful for other reasons as well, but it depends a lot on what else will be going on in the code.
I will try this idea :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.