Advertisement
Luke_G

MATLAB not breaking when r=1

Apr 16th, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. %% test
  2.  
  3. clear
  4. clc
  5. clf
  6.  
  7. moves = {};
  8. % Create a 2D array of size 8x8 containing zeros
  9. a = zeros(8, 8);
  10.  
  11. % Set the pixel at position (3,3) to 1
  12. a(3, 3) = 1;
  13.  
  14. % Determine the size of the matrix a
  15. [y_len, x_len] = size(a);
  16.  
  17. % Nested for loop to iterate through all y and x coordinates
  18. for y = 1:y_len
  19. for x = 1:x_len
  20. % Check if the value at a(y,x) is equal to 1 (gas particle exists)
  21. if a(y, x) == 1
  22. % Choose a random number between 0 and 3
  23. %r = floor(rand*3.9999); % rand gives value between 0 and 1 so multiplying by close to 3.9999 will give values from 0 to 3.9999 which when using float will round to 0 to 3
  24. r = 1;
  25. moves = [moves,r];
  26. % Move the gas particle based on the random number
  27. if r == 0
  28. % Move up
  29. a(y-1, x) = 1;
  30. a(y, x) = 0;
  31. disp('Moving particle up')
  32. break
  33. elseif r == 1
  34. % Move down
  35. a(y+1, x) = 1;
  36. a(y, x) = 0;
  37. disp('Moving particle down')
  38. break
  39. elseif r == 2
  40. % Move left
  41. a(y, x-1) = 1;
  42. a(y, x) = 0;
  43. disp('Moving particle left')
  44. break
  45. elseif r == 3
  46. % Move right
  47. a(y, x+1) = 1;
  48. a(y, x) = 0;
  49. disp('Moving particle right')
  50. break
  51. end
  52. end
  53. end
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement