How can I incorporate values of An and Bn into matrix C? Matrix C output at the end of for loop is still values from X = randn

9 views (last 30 days)
numberOfExperiments = input('Number of trials: '); %number of trials
a = {'G1' 'g1'} %heterozygous die 1 parent
b = {'G2' 'g2'} %heterozygous die 2 parent
X = randn(numberOfExperiments, 2) %random matrix to which assign genome prediction
for rows = 1:numberOfExperiments % # of rows in matrix for genome prediction
i=randperm(length(a),1) %vector with 1 integer 1
A=a(i) %picks one element from a
B=b(i) %oicks one element from b
Anew = cell2mat(A) %converts A to matrix
Bnew = cell2mat(B) %converts B to matrix
if Anew == 'g1'
An = 0
else
An = 1
end
if Bnew == 'g2'
Bn = 0
else
Bn = 1
end
for cols = 1:2 % # of cols in matrix for genome prediction
C(:,cols) = X(An, Bn)
end
C(rows, :) = X(An, Bn)
end %C is matrix with randomly selected elements from a and b to predict offspring's genome

Accepted Answer

SAA
SAA on 19 Jul 2020
You should change An and Bn to 1 and 2, MATLAB's indexing starts from 1 not zero so everytime i = 2 you'll get an error since An and Bn will be 0.
The values of C come from X, so eventually thats what they should be, C will not equal X exactly but all of it's values come from X. Also these lines are overriding each other in each loop, so technically you are only storing the results from the last loop.
The loop that I gave you will store the values at each full iteration, now I am not sure if what you want is to have the same value for the row and column for each full iteration or not but this will give that meaning that you're not losing any data.
Also I don't know why X has that many numbers in it you are only using X(1,1) or X(2,2) not sure if that's what you want or not.
for cols = 1:2 % # of cols in matrix for genome prediction
C(:,cols) = X(An, Bn);
end
C(rows, :) = X(An, Bn);
to store each loop you should use this
for cols = 1:2 % # of cols in matrix for genome prediction
C(rows,cols) = X(An, Bn); % this will not override and will store the values of each loop
end
  4 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!