Advertisement
Luke_G

MATLAB problem

Apr 16th, 2024
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | Source Code | 0 0
  1. clear
  2. clc
  3. clf
  4.  
  5.  
  6. % Create a 2D array of size 8x8 containing zeros
  7. a = zeros(8, 8);
  8.  
  9. % Set the pixel at position (3,3) to 1
  10. a(3, 3) = 1;
  11.  
  12. % Determine the size of the matrix a
  13. [y_len, x_len] = size(a);
  14.  
  15. % Nested for loop to iterate through all y and x coordinates
  16. for y = 1:y_len
  17. for x = 1:x_len
  18. % Check if the value at a(y,x) is equal to 1 (gas particle exists)
  19. if a(y, x) == 1
  20. % Choose a random number between 0 and 3
  21. rtest = floor(rand*3.9);
  22. 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.
  23.  
  24. % Move the gas particle based on the random number
  25. if r == 0 && y > 1
  26. % Move up
  27. a(y-1, x) = 1;
  28. a(y, x) = 0;
  29. elseif r == 1 && y < y_len
  30. % Move down
  31. a(y+1, x) = 1;
  32. a(y, x) = 0;
  33. elseif r == 2 && x > 1
  34. % Move left
  35. a(y, x-1) = 1;
  36. a(y, x) = 0;
  37. elseif r == 3 && x < x_len
  38. % Move right
  39. a(y, x+1) = 1;
  40. a(y, x) = 0;
  41. end
  42. end
  43. end
  44. end
  45.  
  46. % g)
  47. if r == 0
  48. % Move up
  49. disp('Moving particle up')
  50. elseif r == 1
  51. % Move down
  52. disp('Moving particle down')
  53. elseif r == 2
  54. % Move left
  55. disp('Moving particle left')
  56. elseif r == 3
  57. % Move right
  58. disp('Moving particle right')
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement