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!

采纳的回答

Image Analyst
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 CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by