Lec02 Script M File Array

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 40

Computational physics in AI era

Lector 01: introduction

Kyung Taec Kim


3.6 Matlab search path
Ch. 4. Script M file

• You don’t want to type codes again and again.  M-file (matlab code file).

• How to run
• Save and run button
• F5
• Type filename in the command window
Some useful functions …
4.2 BLOCK COMMENTS AND CODE CELLS

• Use % for every lines or %{ }%


4.4 STARTUP AND FINISH

• When MATLAB starts, marlabrc.m and startup.m runs automatically.

• Matlabrc.m
• You don’t need to change this file.
• It contains default features (window size, position, …)
• It runs pathdef.m that contains file paths.
• It runs startup.m.

• Startup.m
• Things that you want to run when you start Matlab.

• When MATLB finishes, finish.m will run automatically.


In Python and C/C++, the index starts from 0.
In Matlab, the index starts from 1.
Chapter 5. Arrays and array operations

• 5.1 Simple arrays • 5.2 Array addressing or indexing

>> x[3]
x[3]

유효하지 않은 표현식입니다 . 함수를 호출하거나 변수를 인덱싱할 때는
소괄호를 사용하십시오 . 그렇지 않으면 , 일치하지 않는 구분 기호가 있는지
확인하십시오 .

Use space, comma for elements


Note that the same indexes are used.
5.3 Array construction
Quiz #1

• Make the following array

• a = [3 5 7 9 11]

• b = [3 5 7 9 ……. 111]

• c = [111 108 105 … 33]

• d = [1, 3, 5 …, 109, 111, 109, … 5, 3, 1]


5.4 Array orientation (Complex) Transpose

(Non complex) dot-transpose


5.6 Array-array mathematics

Use dot for element by element operation!


5.7 Standard arrays
5.8 Array manipulation

The array size has been increased.


Quiz
Access by logic
>> x = [-3 : 3]
5.9 Array sorting
* Random permutation of integers,
Integer numbers selected randomly from 1 to n

Try ‘descend’
5. 10 Subarray searching
5.11 Array manipulation function
>> B = 1 : 12;

Repmat, kron, …
5.12 Array size

Number of element

Length of array (for larger di-


rection)
5.13 Arrays and memory utilization

Allocate 100x100 element array

Deallocate, and allocate 5x6 element array

No change

Deallocate, and allocate 8x6 element array

>> x = 1; tic; for m=1:1000000; x(m) = m; end; toc


경과 시간은 0.125922 초입니다 .

>> x = 1; tic; for m=1:1000; x(m) = m; end; toc


경과 시간은 0.000398 초입니다 .
Quiz

• Make the following matrix

x=

1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

• And, this

x=

0 5 9 13
2 0 10 14
3 7 0 15
4 8 12 0
Homework #01

Change your home directory

>> diary KtKim_HW001.txt


>> Try what we learned this week.
>> diary off

You will see a new file “KtKim_HW001.txt”.

Upload it to LMS system.

You might also like