Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear
- clc
- clf
- % Create a 2D array of size 8x8 containing zeros
- a = zeros(8, 8);
- % Set the pixel at position (3,3) to 1
- a(3, 3) = 1;
- % Determine the size of the matrix a
- [y_len, x_len] = size(a);
- % Nested for loop to iterate through all y and x coordinates
- for y = 1:y_len
- for x = 1:x_len
- % Check if the value at a(y,x) is equal to 1 (gas particle exists)
- if a(y, x) == 1
- % Choose a random number between 0 and 3
- rtest = floor(rand*3.9);
- r = rtest; % <<<<<<<<<<<<<< problem here. When r = rtest then the only values generated are 0 and 2 (so not behaving as it should) however if you set r to equal 1 for example and not rtest then rtest then starts behaving as it should. so somehow by setting r = rtest its changing rtests behaviour.
- % Move the gas particle based on the random number
- if r == 0 && y > 1
- % Move up
- a(y-1, x) = 1;
- a(y, x) = 0;
- elseif r == 1 && y < y_len
- % Move down
- a(y+1, x) = 1;
- a(y, x) = 0;
- elseif r == 2 && x > 1
- % Move left
- a(y, x-1) = 1;
- a(y, x) = 0;
- elseif r == 3 && x < x_len
- % Move right
- a(y, x+1) = 1;
- a(y, x) = 0;
- end
- end
- end
- end
- % g)
- if r == 0
- % Move up
- disp('Moving particle up')
- elseif r == 1
- % Move down
- disp('Moving particle down')
- elseif r == 2
- % Move left
- disp('Moving particle left')
- elseif r == 3
- % Move right
- disp('Moving particle right')
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement