How to add negative number to the and of rand function
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello I have to generate 5 lines of random numbers and at the end of random numbers I have to add one extra negative number from (-1 to -9) I tried strcat and things like that but I couldn't combine negative and real numbers can You help me please? The code below will create everything what I need but I have to somehow combine them. Result has to be for example. Then I have to send them via serial port to the arduino. I know how to do it but sometimes it showed me error. It works only when i send it as a string. I had it like this i = input('Pu numbers: ', '%s'), fwrite(s,i); and it works but now I have to send those random numbers
if true
1 2 3 4 5 -1
5 8 3 5 9 -2
.
.
.
9 2 4 8 6 -9
end
if true
% for b = 1:9
x = round(rand(1,5)*9);
disp(x)
end
for a = -1:-1:-9
disp (a);
end
end
댓글 수: 0
답변 (2개)
Roger Stafford
2016년 4월 21일
편집: Roger Stafford
2016년 4월 21일
What about
x = [floor(10*rand(1,5)),floor(-9*rand)];
(Note: writing "round(rand(1,5)*9)" makes 0 and 9 half as probable as each of the other integers in between.)
Renato Agurto
2016년 4월 21일
Assuming x is your 9x5 random matrix:
out = [x (-1:-1:-9)'];
is this what you want?
댓글 수: 3
Renato Agurto
2016년 4월 21일
yes, but you have 2 separate for loops. Can you give us an example of x... should it be constantly overwritten or is a big matrix?
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!