MATLAB: How to determine if the data is part of one population or another

random sampleseparating elementsStatistics and Machine Learning Toolbox

pop1=randn(1000,1);
>> pop2=randn(9000,1)-1;
>> pop=[pop1;pop2];
>> k=randsample(pop,500);
How do I go from here to put the elements of k that are originally found in pop1 into their own matrix?

Best Answer

Hi Mr Anzulis
since not yet sure the sampling is needed, let's start determining common elements
1.
Simplified version of your input data
pop1=randi([-10 10],5,1)
=
-6
-5
2
-1
-3
pop2=randi([-10 10],10,1)-1
=
6
1
0
8
-5
4
4
-4
0
-10
2.
Merged data
pop=[pop1;pop2]
=
-6
-5
2
-1
-3
6
1
0
8
-5
4
4
-4
0
-10
3.
what elements of pop1 are in pop, a their locations
[pop1_in_pop locs_in_pop locs_in_pop1]=intersect(pop,pop1)
pop1_in_pop =
-6
-5
-3
-1
2
locs_in_pop =
1
2
5
4
3
locs_in_pop1 =
1
2
5
4
3
checking indices match
pop(locs_in_pop)
=
-6
-5
-3
-1
2
pop1(locs_in_pop1)
=
-6
-5
-3
-1
2
Same for pop and pop2
4.
[pop2_in_pop locs_in_pop locs_in_pop2]=intersect(pop,pop2)
pop2_in_pop =
-10
-5
-4
0
1
4
6
8
locs_in_pop =
15
2
13
8
7
11
6
9
locs_in_pop2 =
10
5
8
3
2
6
1
4
Now you know exactly
  • what elements of pop1 are in pop
  • what elements of pop2 are in pop
  • what elements of pop are in pop2
  • what elements of pop are in pop2
And you also know all their respective indices.
5.
to randomly sample, you can use command randsample
length_sample=3
sample1=randsample([1:1:length(pop)],length_sample)
sample1 =
8 12 1
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG