2 views (last 30 days)
Show older comments
George Francis on 21 Apr 2016
-
-
Link
Direct link to this question
https://www.mathworks.ca/matlabcentral/answers/280232-how-to-add-negative-number-to-the-and-of-rand-function
Commented: George Francis on 22 Apr 2016
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 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Answers (2)
Roger Stafford on 21 Apr 2016
Edited: Roger Stafford on 21 Apr 2016
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.)
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
George Francis on 21 Apr 2016
Direct link to this comment
https://www.mathworks.ca/matlabcentral/answers/280232-how-to-add-negative-number-to-the-and-of-rand-function#comment_360628
I have to do it separatly and then add the negative numbers. Also the negative numbers can't be random. It has to be in order from -1 to -9.
1 2 3 4 5 -1
4 7 8 9 3 -2
. -3
. -4
1 4 7 8 9 -5
etc.
Sign in to comment.
Renato Agurto on 21 Apr 2016
⋮
Assuming x is your 9x5 random matrix:
out = [x (-1:-1:-9)'];
is this what you want?
3 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
George Francis on 21 Apr 2016
Direct link to this comment
https://www.mathworks.ca/matlabcentral/answers/280232-how-to-add-negative-number-to-the-and-of-rand-function#comment_360633
It shows me error Dimensions of matrices being concatenated are not consistent.
Also then I have to write each line to the serial port separetly, that's why I have there loop for
Renato Agurto on 21 Apr 2016
Direct link to this comment
https://www.mathworks.ca/matlabcentral/answers/280232-how-to-add-negative-number-to-the-and-of-rand-function#comment_360646
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?
George Francis on 22 Apr 2016
Direct link to this comment
https://www.mathworks.ca/matlabcentral/answers/280232-how-to-add-negative-number-to-the-and-of-rand-function#comment_360781
I don't know how it supposed to look. I thought that I will make in the firts for loop random numbers from 1 to 9 then in another for loop I'll try add negative numbers in order from -1 to -9 and then I will create another for loop with pause which sends this data into serial port one by another.
1 step
1 2 3 4 5
4 7 8 9 3
.
.
1 4 7 8 9
2 step
1 2 3 4 5 -1
4 7 8 9 3 -2
. -3
. -4
1 4 7 8 9 -5
3 step
for p = 1:9
fwrite(serial object, data x);
pause(5);
end
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABMATLAB Support PackagesMATLAB Support Package for Arduino Hardware
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Tags
No tags entered yet.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office