Need assistance updating a Matrix While code is iterating

The Idea behind my code is to take an empty matrix and, based on a series of subroutines, refill it with new values. The issue I'm having is that rather than storing the new values in the matrix and testing this new matrix the code is erasing and overwriting any previous alterations to the matrix. When the code does loop it hits its recursion limit when the goal is to simply fill the empty spaces in the M(x) matrix.
n=5; %matrix size
M=zeros(n);%matrix of all zeros that is being updated
I=1; %number of iterations
C=zeros(size(M));%matrix of random numbers that determine the directional value assigned to each position
D=zeros(size(M));%matrix of positional values
for y=randi(n),x=randi(n);
if M(x,y)==0
C(x,y)=NumberGenerator; %determines what random value is assigned to the matrix postion
if C(x,y)<.2
M(x,y)=0;
D(x,y)=0;
Test; %Name of this code, meant to loop this process
elseif C(x,y)>.2
M(x,y)=1;
MatrixGenV2; %subroutine that generates a directional value at each position based on C(x,y)
BoundaryCondition; %Boundary condition I've placed on the matrix
Test;
end
elseif M(x,y)>0
Test;
end
end
Keep in mind I'm very new to Matlab so if my code isn't as streamlined as it can be that's why. Thank you in advance for any help.

2 Comments

NumberGenerator , MatrixGenV2 , BoundaryCondition , Test??
Those are the subroutines that I'm calling during the code and 'Test' is the name of this code above I have it in the code as a means of looping the process. My fault I should have included those.
Number Generator:
function [r]=RandomNumber(z) %generates a random number that helps determine th direction of a given point on the matrix
zmin=0;
zmax=1;
N=1;
r=zmin+rand(1,N)*(zmax-zmin);
end
Boundary Condition:
if y==1 && D(x,y)==W %if the boundary of the far right edge of the matrix is crossed than a 1 appears in the corresponding location on the far left
M(x,n)=1
M(x,1)=1
elseif y==n && D(x,y)==E %if the boundary of the far left edge of the matrix is crossed than a 1 appears in the corresponding location on the far right
M(x,1)=1
M(x,n)=1
end
if x==1 && D(x,y)==N %if the boundary of the top of the matrix is crossed than a 1 appears in the corresponding location on the bottom
M(n,y)=1
M(1,y)=1
elseif x==n && D(x,y)==S %if the boundary of the bottom of the matrix is crossed than a 1 appears in the corresponding location on the top
M(1,y)=1
M(n,y)=1
end
MatrixGenV2:
%Generates a Directional Matrix assigned to each collapsed value in M(x,y)
W=1;%indicates collapsed molecule in West direction
S=2;%indicates collapsed molecule in South direction
N=3;%indicates collapsed molecule in North direction
E=4;%indicates collapsed molecule in East direction
if C(x,y)<.2000
D(x,y)=0;
elseif C(x,y)>.8
D(x,y)=E;
elseif (.6>C(x,y))&&(C(x,y)>.4)
D(x,y)= W;
elseif (.8>C(x,y))&&(C(x,y)>.6)
D(x,y)= S;
elseif(.4>C(x,y))&&(C(x,y))>.2
D(x,y)= N;
end

Sign in to comment.

 Accepted Answer

There are several problems in this for-loop.
for y=randi(n),x=randi(n);
This is actually executed as two lines of code the first: y=randi(n) and the second: x=randi(n). What I think you want to do is execute these two lines within a loop but I don't know what you're loop over. I noticed your variable "I" isn't used and there's comment "number of iterations" so perhaps you want.
for j = 1:I
y=randi(n),
x=randi(n);
...
end
Since x and y are random integers, you'll likely continue to overwrite values in the matricies that use X and Y as indices since it's possible for the same number of be chosen randomly. One way around that is to use randsample() which can sample integers without replacement.

2 Comments

Thank you so much this did help alot. Ideally even if the same position is chosen through the randomization the code shouldn't run the NumberGenerator, MatrixGen or BoundaryCondition subroutines. If I'm mistaken please let me know! One follow-up,say I run the code and it produces the matricies I'm looking for lets call them P. How would you suggest running the P through my code again? Would I need an additional subroutine or simply add a few mor lines to the current code?
Thank you again this is exactly what I needed!
I don't know what your test() functrion does but it looks like it's possible that if M(x,y)>0 then a new (x,y) is chosen. This is really inefficient and the randsample() function is a better approach.
Regarding your followup, I'm not sure what you mean by running P back through your code. I'm assuming there's a section of code that creates P and you want to run P through that section again after P is created. Here's an example of what I'm imagining.
function foobar()
xxy = [1 2 3; 4 5 6];
qwe = [0 0 0; 1 1 1];
P = nestFunc(xxy, qwe);
P2 = nestFunc(xxy, P);
function r = nestFunc(m, n)
r = m + n;
end
end

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 28 Nov 2018

Commented:

on 30 Nov 2018

Community Treasure Hunt

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

Start Hunting!