need help using the rand function.

1 回表示 (過去 30 日間)
Kel
Kel 2014 年 3 月 16 日
コメント済み: Kel 2014 年 3 月 16 日
hey guys i have got Matlab2010a and i am trying to implement the code below but i can't seem to get it to work. when i run it in matlab, it comes up with an error saying: ??? Error using ==> randperm Too many input arguments.
fo=4 fs=100 ts=1/fs t=0:ts:1-ts n=length(t) y=2*sin(2*pi*fo*t) plot(t,y) numberOfSamplesToTake = 20; sampleIndexes = randperm(numel(y), numberOfSamplesToTake) % Plot the samples; ts = t(sampleIndexes) ys = y(sampleIndexes) hold on; plot(ts, ys, 'r*');

採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 3 月 16 日
Use:
numberOfSamplesToTake = 20;
a = randperm(numel(y));
sampleIndexes = a(1:numberOfSamplesToTake);
  1 件のコメント
Kel
Kel 2014 年 3 月 16 日
worked perfectly. i have been struggling with that for a long time. thank you so much.

サインインしてコメントする。

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2014 年 3 月 16 日
In recent versions of MatLab, randperm(N,M) randomises the numbers 1:N and takes the first M values. In older versions (like 2010a) you can mimic this functionality by replacing it with these two steps:
A = randperm(N) ;
A = A(1:M) ;

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by