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 个评论
更多回答(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!