Fill in matrix using nested loop
6 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I am struggling to create the xyFit matrix below.
What I am trying to do is to fill in "0" when xx is below the xcrit value (3) and to fill in "1" when xx is at or above the xcrit value.
Here is what I have come up with so far:
Cap = 10;
xcrit = 3;
xx = Cap;
yy = Cap;
xyprob = ones(xx,yy)*0.1; %probability individual A encounters opponent B
xyFit = zeros(xx,yy); %building fitness matrix
for ii = 1:xx
for jj = 1:yy
if ii == 1:(xcrit-1)
xyFit(ii,jj)==0
else
xyFit(ii,jj)== 1
end
end
end
Any help would be greatly appreciated!
0 件のコメント
採用された回答
Star Strider
2020 年 8 月 4 日
I am not certain what you want to do.
Try this:
Cap = 10;
xcrit = 3;
xx = Cap;
yy = Cap;
xyprob = ones(xx,yy)*0.1; %probability individual A encounters opponent B
xyFit = zeros(xx,yy); %building fitness matrix
for ii = 1:xx
for jj = 1:yy
if ii < xcrit
xyFit(ii,jj) = 0;
else
xyFit(ii,jj) = 1;
end
end
end
Theere are probably easier ways to do what you want (for example using ‘logical indexing’). Aside from that, note that the double-equal == is for the logical comparison for equailty, not for assignment. To assign a value to a variable, use only =.
.
2 件のコメント
Star Strider
2020 年 8 月 4 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!