Plotting a digital polymer chain.

15 ビュー (過去 30 日間)
Sydney Carrow
Sydney Carrow 2021 年 3 月 15 日
コメント済み: darova 2021 年 3 月 18 日
I got the instructions from my professor and have no idea how to complete the code. The A beads should be red and the B beads should be blue.
1) Create two empty 30 x 2 matrices; one matrix for A beads, the other for B beads.
2) Open a for loop from index i=1:30
3) Calculate a random number
4) If the random number is <=0.5, then for the A matrix, change the i,1 element to the current index in the for loop, and the i,2 element to a value of 1.
If the random number is >0.5, then for the B matrix, change the i,1 element to the current index in the for loop, and the i,2 element to a value of 1.
5) Repeat steps 3 and 4 all the way up to i = 30
6) Close the for loop
7) Remove all the zero rows from the A and B matrices
8) Plot the beads
  1 件のコメント
darova
darova 2021 年 3 月 15 日
Please show your attempts

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

回答 (1 件)

Sydney Carrow
Sydney Carrow 2021 年 3 月 15 日
I don't have any attempts because I do not know how to do any of it. I can't figure out how to make empty matrices. Once creating the loop, I do not not how to do what he said.
  7 件のコメント
Sydney Carrow
Sydney Carrow 2021 年 3 月 17 日
編集済み: darova 2021 年 3 月 18 日
This is the code I have created. Now though, the index isn't transfering to A or B as the current index when needed. What can I do?
%chain 1
A=zeros(30,2);
B=zeros(30,2);
for i=1:30
if A(i,1)==i || rand<=0.5
A(i,2)=1;
A(i,1)=i;
else
B(i,1)=i;
B(i,2)=1;
if B(i,1)==i || rand<=0.5
A(i,1)=i;
A(i,2)=1;
else
B(i,1)=i;
B(i,2)=1;
end
end
end
%Deletes all zeros in A
TestForZero = A(:,1) == 0;
A(TestForZero, :) = []
%Deletes all zeros in B
TestForZero = B(:,1) == 0;
B(TestForZero, :) = []
%graphs
hold on
plot(A(:,1),A(:,2),'o','MarkerFaceColor',"red",'MarkerSize',15)
plot(B(:,1),B(:,2),'o','MarkerFaceColor',"blue",'MarkerSize',15)
title("Digital Copolymerchain 4")
axis off
hold off
%Deletes all zeros in A
TestForZero = A(:,1) == 0;
A(TestForZero, :) = []
%Deletes all zeros in B
TestForZero = B(:,1) == 0;
B(TestForZero, :) = []
%graphs
hold on
plot(A(:,1),A(:,2),'o','MarkerFaceColor',"red",'MarkerSize',15)
plot(B(:,1),B(:,2),'o','MarkerFaceColor',"blue",'MarkerSize',15)
title('Digital Colymerchain 1')
axis off
hold off
darova
darova 2021 年 3 月 18 日
Here are some corrections
You don't need to check A(i,1)==i part
if A(i,1)==i || rand<=0.5
You don't need red square part (it's the same as above)

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by