Creation new arrays in for loop command - randomizer

Dears,
Currently I am working on some kind of randomizer where I generate white and black balls. In the first step I generate them on the graph as a horizontal "curve". In next step I generated similar "random curve", however, it is vertical curve and it starts only when in horizontal curve black ball appears:
Variable1 = 60;
Variable2 = 30;
% Arrays for the backbone
A = zeros(Variable1,2); %triol
B = zeros(Variable1,2); %dicarboxyacid
% Horizintal randomizer
for i=1:Variable1
if randi(100,1,1)<=Variable2
A(i,2)=1;
A(i,1)=i;
else
B(i,1)=i;
B(i,2)=1;
end
end
% Deletes all zeros in A
TestForZero = A(:,1) == 0;
A(TestForZero, :) = [];
% Vertical randomizer
for q = 1:height(A)
if randi(100,1,1) <= Variable2
C1(q,2) = q+1;
C1(q,1) = A(1,1);
else
C2(q,2) = q+1;
C2(q,1) = A(1,1);
end
end
for q = 1:height(A)
if randi(100,1,1) <= Variable2
D1(q,2) = q+1;
D1(q,1) = A(2,1);
else
D2(q,2) = q+1;
D2(q,1) = A(2,1);
end
end
% Visualization of the randomizer
figure(1)
hold on
plot(A(:,1),A(:,2),'o','MarkerFaceColor',"black",'MarkerEdgeColor',"black",'MarkerSize',10)
plot(B(:,1),B(:,2),'o','MarkerFaceColor',"white",'MarkerEdgeColor',"black",'MarkerSize',10)
plot(C1(:,1),C1(:,2:end),'o','MarkerFaceColor',"black",'MarkerEdgeColor',"black",'MarkerSize',10)
plot(C2(:,1),C2(:,2:end),'o','MarkerFaceColor',"white",'MarkerEdgeColor',"black",'MarkerSize',10)
plot(D1(:,1),D1(:,2:end),'o','MarkerFaceColor',"black",'MarkerEdgeColor',"black",'MarkerSize',10)
plot(D2(:,1),D2(:,2:end),'o','MarkerFaceColor',"white",'MarkerEdgeColor',"black",'MarkerSize',10)
axis off
hold off
As you can see, I managed to make vertical curves for more than one black ball in the horizontal curve. Nevertheless, I did it "manually". Is it possible to make it in some for loop?
The problem is I do not know how many of black balls will be created for each run. Moreover, in theory its amount is infinity, because by increasing Variable1 and Variable2 I increase the amount of black balls in the curves (in horizontal as well as vertical curves).
I thought about creating new arrays for each itteration, however, I found information that it is not preffered way and it can be done in other ways. Could you please help me with that?
Thank you in advance,
Cheers

2 件のコメント

Wojciech
Wojciech 2024 年 2 月 18 日
Thank you!! :) Now it is more clear and I found the solution. Cheers!

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeRandom Number Generation についてさらに検索

製品

リリース

R2023b

質問済み:

2024 年 2 月 18 日

コメント済み:

2024 年 2 月 18 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by