0

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

1 2 3 4 5 -1
5 8 3 5 9 -2
.
.
.
9 2 4 8 6 -9

for b = 1:9    
    x = round(rand(1,5)*9);
    disp(x)
end
for a = -1:-1:-9


    disp (a);
end
  • 1
    Why would you use [`strcat`](http://www.mathworks.com/help/matlab/ref/strcat.html), a function that concatenates *strings*, on numbers? Use [`cat`](http://www.mathworks.com/help/matlab/ref/cat.html). Also, negative numbers are real numbers... – sco1 Apr 21 '16 at 12:32

3 Answers3

1
[round(rand(9,5) * 9), -(1:9)']
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
0
r = randi(9,9,5);
n = (-1:-1:-9)';

a = [r,n]
Dan
  • 45,079
  • 17
  • 88
  • 157
  • It show me error Dimensions of matrices being concatenated are not consistent. – Franta123456 Apr 21 '16 at 15:20
  • @Franta123456 try now – Dan Apr 21 '16 at 15:21
  • Ok now it works. And is here some way how to separate each line. Because I have to send each line into the serial port. That's why I tried used function for. And also when I will need into serial port 10 lines. The negative numbers has to start again from begging. For example 4 4 5 7 6 -1 . . . 5 7 3 6 6 -9 9 1 3 5 3 -1 etc. – Franta123456 Apr 21 '16 at 16:15
  • @Franta123456 that sounds like a completely different question. I suggest you ask it as a new question. Also don't say line, it's ambiguous. Rather refer to rows or columns in this case. – Dan Apr 21 '16 at 16:43
0

I read your code first and it seems like your first batch of random number ranges from 0 to 9 and they must be integers and your second batch (the last column) ranges from -1 to -9 and they are also integers. If so, Try this

A = [round(rand(5,5)*9),round(-1+rand(5,1)*-8)];
disp (A)

in the expression:

round(rand(5,5)*9) generate 5 by 5 random number matrix between 0 and 9.

Generally, if you wish to generate n-by-m matrix of random integer between a and b, then use the expression below:

round(a+rand(n,m)*(b-a))

Hope it helps.

Jackson

Jackson
  • 1
  • 2
  • I have to do it separatly and then add the negative numbers at the end. Also the negative numbers can't be random. It has to in order from -1 to -9. And then I have to write into my serial port. That's why I have there for loop, because if I have to write data more than 9 times the negative numbers should go again from -1 to -9 – Franta123456 Apr 21 '16 at 15:14
  • In this case, Dan's method should work. or change my code into the following: `A = [round(rand(9,5)*9),transpose(-1:-1:-9)]; disp (A)` – Jackson Apr 21 '16 at 15:17
  • Tried that but it showed me error Dimensions of matrices being concatenated are not consistent. – Franta123456 Apr 21 '16 at 15:20
  • The reason why I have for loop in my program is because I have to send to the serial port each line separetly. In my case (for b = 1:9) 9 lines which I will send to the serial port so I have to add there fwrite(serialobjet, and data with negative number). When the device on the end of serial port will need more then 9 lines. The negative numbers should be repeated. 12345 -1 45871 -2 . . . 65748 -9 56874 -1 32164 -2 etc. – Franta123456 Apr 21 '16 at 15:30