CodeCodeCode

ENGR132: HW06 - array_name

May 6th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.87 KB | None | 0 0
  1. function [new_array] = array_name(old_array, div)
  2.  
  3. % FUNCTION: Checks an array for negative values, which will be
  4. % squared, and values divisible by "div", which will be multiplied by 2.
  5. %
  6. % INPUTS:
  7. % 1) old_array: The array given by the user.
  8. % 2) div: The value given to check for divisibility.
  9. %  
  10. % OUTPUTS:
  11. % 1) new_array: The new array for output.
  12.  
  13. %Find the dimensions of the given array.
  14. [row, column] = size(old_array);
  15. %Fills new array with zeros.
  16. new_array = zeros(row,column);
  17.  
  18. %For loop counts along the columns of user's vector.
  19. for count_column = 1:1:column
  20.     %For loop counts along the rows of user's vector.
  21.     for count_row = 1:1:row
  22.         %If the value is negative, square it.
  23.         if old_array(count_row, count_column) < 0
  24.             new_array(count_row, count_column) = old_array(count_row, count_column)^2;
  25.         %If the value is divisible by the user's number, multiply by 2.
  26.         elseif rem(old_array(count_row, count_column), div) == 0
  27.             new_array(count_row, count_column) = old_array(count_row, count_column) * 2;
  28.         %For everything else, do nothing.
  29.         else
  30.             new_array(count_row, count_column) = old_array(count_row, count_column);
  31.         end %If (value check) end
  32.     end %For (row) end
  33. end %For (column) end
  34.  
  35. % [new_array] = array_name([5, 17, -3, 8, 0, -7, 12], 4)
  36. %
  37. % new_array =
  38. %
  39. %      5    17     9    16     0    49    24
  40. %
  41. % [new_array] = array_name([1 5 3; -2 6 -7; -4 12 24], 2)
  42. %
  43. % new_array =
  44. %
  45. %      1     5     3
  46. %      4    12    49
  47. %     16    24    48
  48. %
  49. % [new_array] = array_name([23 17 -34 66; -4 33 -23 0; 12 7 11 22; -2 0 0 7], 11)
  50. %
  51. % new_array =
  52. %
  53. %           23          17        1156         132
  54. %           16          66         529           0
  55. %           12           7          22          44
  56. %            4           0           0           7
Advertisement
Add Comment
Please, Sign In to add comment