new in matlab, first question

1 Ansicht (letzte 30 Tage)
googo
googo am 20 Mär. 2013
hey, i'm trying to take a sample from a raw vector using myRandPerm. i had started in the folowing way:
function s=sample(x,n,seed)
if n>length(x)
-1
else
{don't know how exactly i can get myrandperm selecting n numbers from the x vector}
end
end
thank you...
  2 Kommentare
Dani Tormo
Dani Tormo am 20 Mär. 2013
Bearbeitet: Dani Tormo am 20 Mär. 2013
Try to format the code for an ease reading. You have more info on the Help button up here.
Jan
Jan am 21 Mär. 2013
Bearbeitet: Jan am 21 Mär. 2013
What is your reason to implement myRandPerm instead of using the builtin randperm function?
The tags are used to classify the questions. Therefore "matlab" is not useful, because all questions concerns this topic. "randperm" would be much better.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Sean de Wolski
Sean de Wolski am 20 Mär. 2013
Welcome!
You can use randperm
doc randperm
or do what randperm() does under the hood:
[~,idx] = sort(rand(1,10))
  2 Kommentare
googo
googo am 20 Mär. 2013
thanks but what do you mean by doc randperm?
Sean de Wolski
Sean de Wolski am 20 Mär. 2013
At the MATLAB command prompt run:
>> doc randperm
It will bring up the documentation. FYI: In this forum, gray text like that represents code to be executed.

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 21 Mär. 2013
From the help:
p = randperm(n,k) returns a row vector containing k unique integers selected randomly from 1 to n inclusive.
So
x = rand(1,20) % Generate sample data.
n = 5; % Take 5 from x at random locations
% Get the random locations.
xIndexes = randperm(numel(x), n);
% Extract x from those random locations.
xSubSample = x(xIndexes)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by