我需要创建程序,它给我5个随机数(从0到9,整数)。然后在第二部分我必须在这些随机数的末尾添加一个负数,从-1到-5。然后通过蓝牙将它们发送到我的arduino。我知道如何解决第一步(5个随机数)和最后一个(通过蓝牙写它们)。但是例如当我发送这些数据6次时。它必须看起来像这样(负数应该从头开始)。
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
我现在使用的程序。 清除所有; 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);
答案 0 :(得分:0)
使用randi()
生成序列会更容易:
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
生成N
行:
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