using rand to generate numbers

조회 수: 1 (최근 30일)
PChoppala
PChoppala 2013년 9월 3일
Hi
I have a simple question. I understand that both a and b vectors (please see below) will generate 5 random numbers from 0 to 5 with an interval of 1 between each number. But I would like to know if there is any difference between vectors a and b or if they have the same meaning.
N=5;
a = [0:N-1]+rand(1,N)/N;
b = [0:N-1]+rand/N;
Thank you

채택된 답변

the cyclist
the cyclist 2013년 9월 4일
Is this homework?
If so, I suggest you read
doc rand
and see what the difference is between the syntax
rand(1,N)
and
rand
  댓글 수: 2
PChoppala
PChoppala 2013년 9월 4일
Well, I do know the difference .. that
rand %gives a number between 0 and 1
rand(1,N) %gives N numbers between 0 and 1
But I wanted to know if there is any technical difference in vectors a and b. I guess they are the same, but wanted to confirm.
the cyclist
the cyclist 2013년 9월 4일
Here's a hint. Compare
diff(a)
to
diff(b)

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

추가 답변 (1개)

Shashank Prasanna
Shashank Prasanna 2013년 9월 4일
편집: Shashank Prasanna 2013년 9월 4일
Try executing it and seeing it for yourself.
rand just generats one random number and adds it to each element of the vector: [0:N-1]
where as rand(1,N) generates N random numbers and adds them element wise to the vector: [0:N-1]
They are obviously different. Is that what you are looking for?
  댓글 수: 1
PChoppala
PChoppala 2013년 9월 4일
Yeah I understand this .. the more I think, the more easy it becomes .. guess this question should be deleted

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by