how to randomly choose some values from specific variable

consider a=[6 5 4 3 6 4 2 1]
i want some values from variable 'a' say[4 3 2]or 20% of variable a

Answers (1)

E.g., if you want only a subset of your vector:
k = number of values you want; % Must be <= numel(a)
p = randperm(numel(a),k);
b = a(p);
If you want a sample that could include repeats, e.g.,
k = number of values you want;
p = randi(numel(a),1,k);
b = a(p);

2 Comments

also
values=s(end,randperm(size(s,2), 3)
thank you very much
OK -- you didn't say anything about "s" or a 2D matrix, but I think you should mark it "Accepted" anyway, to give James reputation points.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 15 Jun 2015

Commented:

on 15 Jun 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!