How to store a data from a nested for loop in a n x n matrix
1 次查看(过去 30 天)
显示 更早的评论
here's my code:
for j=1:tmax
for i=1:n
if mat(i)==0 && rand() < r*(1-(zerfre)^2);
idx = 1 + fix(rand(1,1)*numel(mat));
mat(i)=mat(idx);
elseif mat(i)==1 && rand() < r*(1-(onefre)^2);
idx = 1 + fix(rand(1,1)*numel(mat));
mat(i)= mat(idx);
elseif mat(i)==2 && rand() < r*(1-(twofre)^2);
idx = 1 + fix(rand(1,1)*numel(mat));
mat(i)= mat(idx);
elseif mat(i)==3 && rand() < r*(1-(thrfre)^2);
idx = 1 + fix(rand(1,1)*numel(mat));
mat(i)= mat(idx);
elseif mat(i)==4 && rand() < r*(1-(foufre)^2);
idx = 1 + fix(rand(1,1)*numel(mat));
mat(i)= mat(idx);
elseif mat(i)==5 && rand() < r*(1-(fivfre)^2);
idx = 1 + fix(rand(1,1)*numel(mat));
mat(i)= mat(idx);
end
last(i,i)=?
end
Basically, I have a nxn matrix (which is mat here) with entries as integers from 0 to 5. I'm going to update each entry of the matrix with the rules written in my for loop.
as an output I want a nxn matrix (which I called last here) with the entries updated after tmax iterations with the written rules. So, it's storing data from a for loop. But I can't figure out how I would construct the output nxn matrix. Thanks a lot!
0 个评论
采纳的回答
Image Analyst
2013-4-3
You can pull
idx = 1 + fix(rand(1,1)*numel(mat));
mat(i)= mat(idx);
out of each if block since it occurs identically in all of them, which leaves your if block basically having no functionality. I have no idea what you want "last" to be so I can only guess. How about last = mat?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!