new in matlab, first question

조회 수: 1 (최근 30일)
googo
googo 2013년 3월 20일
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
Dani Tormo
Dani Tormo 2013년 3월 20일
편집: Dani Tormo 2013년 3월 20일
Try to format the code for an ease reading. You have more info on the Help button up here.
Jan
Jan 2013년 3월 21일
편집: Jan 2013년 3월 21일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Sean de Wolski
Sean de Wolski 2013년 3월 20일
Welcome!
You can use randperm
doc randperm
or do what randperm() does under the hood:
[~,idx] = sort(rand(1,10))
  댓글 수: 2
googo
googo 2013년 3월 20일
thanks but what do you mean by doc randperm?
Sean de Wolski
Sean de Wolski 2013년 3월 20일
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.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2013년 3월 21일
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)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by