MATLAB: How to store a data from a nested for loop in a n x n matrix

forloop

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!

Best Answer

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?