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
조회 수: 13 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
SAA
2020년 7월 19일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!