# MATLAB 03-1 (Array)

신예진·2022년 1월 27일
0

목록 보기
4/17

1) Array Editor (Variable Editor)

① Double-click a variable to see and change its contents in the Array Editor
② Change values of array elements

* Array Editor can be useful when copying data from Excel file


2) empty array

used for ① initializing an array ② removing array elements
x = [] % 0byte 임에도 불구하고 empty array로 존재
x = [x, 3] % repeat this

3) accessing elements of matrix (2D array)

A = rand(6,4)
A(2,3) % array(row,col)

4) accessing elements of vector (1D array)

r = rand(10,1)
r(3) % works for row and column arrays
r(3,1) % 세번째 행, 첫번째 열
r(2:4) % 둘,셋,네번째 행까지
r([1 5 2 2 1])
r(4) = []
r(0) % matlab 은 1-based 이므로 오류 발생

5) size of vector and matrix

r % 1D array
length(r) % for 1D array
[m,n] = size(A) % for 2D array, m&n이라는 변수 이름은 자유
m = size(A,1)
m = size(A,2)

6) column major

A = rand(4,5) % 2D matrix
A(11) % what? A is not 1D vector but 2D matrix. Isn't A(,) right?
% in CPU memory, all arrays (1D, 2D, 3D, ...) are stored as 1D

7) accessing part of array

A
A(2:3, 1:2) % same as A([2,3],[1,2])
A([1 3],[2 4]) % A(1,2) A(1,4); A(3,2) A(3,4)

8) end as array index

A(3,2:end)

9) ":" in array index means all elements (same as 1:end)

A(1,:) % 1st row
A(:,2:3) % 2nd and 3rd columns

10) matrix(2D)-to-vector(1D) conversion

A = rand(3,4);
a = A(:) % same as a = A(1:12) % column major

Q : what if you want row-major conversion?
A : AA = A'; AA(:)

11) vector(1D)-to-matrix(2D) conversion

two steps ① set dimension of matrix ② convert
B = zeros(6,2) % any matrix desired dimension
B(:) = a; % conversion with column major

Q : what if you want (12x1) conversion to (4x3) in row major?
A : B = zeros(3,4); B(:) = a; B = B';

12) Deleting rows and columns

A = rand(4,6)
A(:,2) = []
try A(1,2) = [] % 오류, 0으로 채워넣는 것은 가능

13) Multi-dimensional arrays

A
A(:,:,2) = rand(4,5)
A(:,:,1,2) = rand(4,5)

14) scalar + array

a = rand(6,5)