Matrices and arrays Pekar Miriam
What is matrices? In MATLAB environment, a matrix is a rectangular array of number Vector - 1 by 1 row/ column matrix
Creating matrices matr1=[1 2] matr2=[2 4,4 5] matr2 = Check lines ; matr[] – empty matrix matr = matr3=[3 4;2 3] matr3 = matr1=[1 2] matr1 = 1 2 matr2=[2 4,4 5] matr3=[3 4;2 3] matr4=[2^4,3, 5,4*6+5, 7,[2,3]]
Creating matrices with functions We need to definite the variables first! >> matr_by_matr=[matr_by_vec,2*matr_by_vec] matr_by_matr = >> vec=[1 3 5] vec = >> matr_by_vec=[vec;4+2*vec] matr_by_vec =
Matrices length >> vec=[2 4 5; 4 7 6]; >> [row,col]=size(vec) row = 2 col = 3 Vector length >> vec=[2 4 6]; >> L=length(vec) L = 3
Function rand creating random matrices >> rand_mat=rand(2,0) >> rand_mat=rand(2) >> rand_mat=rand(2,3) Fix - Round toward Zero - >> a=[ ; ; ]; >> fix(a); Randn – random with negative digits rand_mat = rand_mat = rand_mat = Empty matrix: 2-by-0
Indexing into a matrix a(1:2,3:4)=[3 6;8 4] Changing Matrix cells till last cell – with “end” - A(:,end)=[1;2;3;4;5]; Changing value of cell 10 a(1,1)=10 : - changing all cells a(:,3)=1 1 1 Changing a sub matrix
The load function 1 File -> open -> data.dat Create a data.dat file
File -> new -> M-Files M-Files >> data >> whos Name Size Bytes Class Attributes A 2x3 48 double Save in your directory Call to the file The load function 2
Workspace Easy watch all vectors and Matrices ans – Optional Variable Matr=zeros(row,col) Matr=ones(row,col) Transpose ‘ change row matrix to column matrix