0

I need to create program which gives me 5 random numbers (from 0 to 9, integers). Then In second part I have to add at the end of those random numbers one negative in order from -1 to -5. And then send them via bluetooth to my arduino. I know how to solve the first step(5 random numbers) adn the last(write them via bluetooth). But for example when I have send these data 6 times. It has to look like this (negative numbers should go again from beginning).

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

Program which I use right now. clear all; clc;

b = Bluetooth('HC-05', 1);
fopen(b);

x = round(rand(1,5)*9);
a = num2str(x);        
    disp(x)

    fwrite(b,a);


    p = fscanf(b, '%s');
    disp(p);

fclose(b);
  • How does this differ from [your previous question](http://stackoverflow.com/questions/36766995/how-to-add-number-at-the-end-of-rand)? – sco1 Apr 22 '16 at 11:28

1 Answers1

0

It will be easier to use randi() to generate your sequence instead:

x = [randi([0 9], 1, 5) randi([-5 -1])]; %//generate 1x5 vector of random int 0 to 9, and 1x1 value of random int -5 to -1

To generate N rows:

x=[]; neg = 0;
for a=1:N
   neg = neg - 1;
   if (neg < -5)
       neg = -1; %//wrap around
   end
   x = [x; [randi([0 9], 1, 5) neg]]; %//add new row of random numbers to x
end
Lincoln Cheng
  • 2,263
  • 1
  • 11
  • 17
  • The negative numbers can't be random they have to be in order. As you can see in my example in the top. Also can I do that separetly? 1 step create random numbers and then in another step add negative number? – Franta123456 Apr 22 '16 at 12:29
  • @Franta123456 yes, you can do it separately. Also I have edited the answer for the negative number :) hope it helps – Lincoln Cheng Apr 22 '16 at 12:49
  • Thank's a lot. And do you know where should I put fwrite function? I put it behind x = [x; [randi([0 9], 1, 5) neg]]; fwrite(b,x) and it sends me only 1 line not 5 then matlab show me error: An error occured during writing. – Franta123456 Apr 22 '16 at 13:37
  • @Franta123456 About the writing part, that I'm not too sure. – Lincoln Cheng Apr 22 '16 at 15:11
  • I'll try create new question. But you really helped me. – Franta123456 Apr 22 '16 at 17:01