Advertisement
acarrunto

matlab full course

Jul 20th, 2021 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. all variables in MATLAB are matrix
  2.  
  3. >> x = 5
  4. x =
  5. 5
  6.  
  7. >> size(x) % to know the lenght of the variable
  8. ans =
  9. 1 1
  10.  
  11. >> v = [1 2 3]
  12. o % both are valid expression
  13. >> v = [1, 2, 3]
  14.  
  15. >> size(v)
  16. ans =
  17. 1 3
  18.  
  19. creating a matrix variable
  20. >> a = [1 2; 3 4]
  21. a =
  22. 1 2
  23. 3 4
  24.  
  25. >> size(a)
  26. ans =
  27. 2 2
  28. ----------------
  29. you know that v = [1 2 3] which size was 1 3
  30.  
  31. >> v = v'
  32. v =
  33. 1
  34. 2
  35. 3
  36.  
  37. >> size(v)
  38. ans =
  39. 3 1
  40. ----
  41. access elements of the matrix
  42. example vector -> a = 1 2
  43. 3 4
  44. >> a(1,1)
  45. ans =
  46. 1
  47.  
  48. >> a(1,2)
  49. ans =
  50. 2
  51. >> a(2,1)
  52. ans =
  53. 3
  54.  
  55. -------------
  56. access a vector within a matrix
  57. example vector -> a = 1 2
  58. 3 4
  59.  
  60. >> a(1,:)
  61. ans =
  62. 1 2
  63.  
  64. >> a(:,2)
  65. ans =
  66. 2
  67. 4
  68.  
  69. >> a(:,:)
  70. ans =
  71. 1 2
  72. 3 4
  73. ----------------------
  74. access a range in the matrix
  75. example vector -> x = 1 2 3 4
  76. 5 6 7 8
  77. 9 10 11 12
  78. 13 14 15 16
  79.  
  80. >> x(2:3,2:3)
  81. ans =
  82. 06 07
  83. 10 11
  84.  
  85. ----------------------
  86. the : syntax can create a range
  87. (create a row vector with the elements 1 to 10)
  88. >> w = 1:10
  89. w =
  90. 1 2 3 4 5 6 7 8 9 10
  91.  
  92. >> size(w)
  93. ans =
  94. 1 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement