Reputation: 2263
I have 2 vectors (n and t) eg:
n t
1 5
5 3
5 2
2 6
2 9
Once I sample from vector n through randsample(n,1)
, I want to sample from the vector t but only from the values corresponding to that same one in vector n.
eg. If I drew a value of 2 from n, I then want to draw the value 6 or 9 from t. But how do I tell matlab to do that?
Upvotes: 1
Views: 1494
Reputation: 5014
Simple one-liner, assuming you have them stored in a Nx2
matrix
nt = [
1 5;
5 3;
5 2;
2 6;
2 9];
meaning:
n = nt(:,1);
t = nt(:,2);
you can sample nSamples
with replacement by randmonly indexing the matrix row-wise, i.e.:
nSamples = 5;
keepSamples = nt(randi(length(nt),nSamples,1),:);
Upvotes: 1
Reputation: 33854
You could potentially do this:
out = t(n == randsample(n, 1))
This will create a filter based on whether n = its own random sample, ie if
randsample(n, 1) = 2
(n == randsample(n, 1)) = [0
0
0
1
1]
and applying this to t ie:
t(n == randsample(n, 1)) = [6
9]
which are the two corresponding values to 2 in n, but in t.
Hope this helps.
PS if you need just one value from t then you can randsample the output that this function gives you.
Upvotes: 2