Clear Filters
Clear Filters

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

3 views (last 30 days)
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!

Accepted Answer

Image Analyst
Image Analyst on 3 Apr 2013
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?
  1 Comment
Edgaris Rhomer
Edgaris Rhomer on 3 Apr 2013
Thanks for such a prompt reply! Yes, that is precisely what I ended up doing haha. Thank you very much!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!