CZ1102 Computing & Problem Solving Lecture 4
CZ1102 Computing & Problem Solving Lecture 4
Week 5
By Ji Hui
Array
• Why we need array
– Batch inputting data for processing
• Vector
– Also called list, arrays in other language
– Row vector
– Column vector
(Cont’)
• Matrix
– Multi‐dimensional array
1, 2, 3, 4
5, 6, 7,8
– Vectors are special cases of matrices with one
single column or one single row
– Matrix and column vector will be discussed in
future lectures on numerical linear algebra
Vector initialization
• Initialize a vector
>>x=[ 1 3 0 ‐1 5]
x =
1 3 0 ‐1 5
>>whos
Name Size Bytes Class Attributes
x 1x5 40 double
More ways of initialization
• More ways to initialize vector
– Can use comma to separate items as you like
>>x=[1,3,0,‐1,3]
– Don’t forget the commas (or spaces) between
elements, otherwise something unexpected will
happen
>>x=[130,‐13]
– Can use one vector in a list for another one
>>a=[1 3 0];
>>b=[‐1,3];
>>c=[a,b]
Initializing vectors using the colon
operator
• A vector can also be generated (initialized)
with the colon operator
• Integer
– j:k
a vector with elements j, j + 1, j + 2, . . . , k
– J:m:k
• when m> 0, a vector with elements j, j +m, j +2m, . . . ,
such that the last item <=k
• when m<0, a vector with elements j, j+m,j+2m, …, such
that the last item >=k.
Example
• Some examples
>> x=1:3:6
x =
1 4
>> x=0.1:‐0.3:1.0
x =
0.1000 ‐0.2000 ‐0.5000 ‐0.8000
• What happen when k<j
>> x= 1:3:‐1
x =
Empty matrix: 1‐by‐0
Transposing vectors
• So far all vectors are row vectors, how to get
column vectors
Using dash ‘ or using semicolon ;
>>x=[1,2,3]’ >>x=[1;2;3]
x = x =
1 1
2 2
3 3
How to access items in vector
• Subscript
x(k): k‐th element of vector x
>>x=[1,3,‐1,0,2];
>>x(4)
ans =
0
• A subscript is indicated inside round brackets
(also called ‘parentheses’).
• A subscript may be a scalar or a vector.
• In MATLAB subscripts always start at 1.
(cont’)
– x(j:k): the vector of j‐th, (j+1)‐th, …, k‐th elements
of vector x
>>x(2:4)
ans =
3 ‐1 0
– What about r(1:2:5)?
– What about r([1 2 5])?
Repetition
• Why we need repetition?
– Batch processing the inputted data
• Example
(a) find the square of i=1,2,3,4,5,…10?
– Without repetition
>> sqrt(1)
>> sqrt(2)
>> sqrt(3)
.
.
>>sqrt(10)
– Too many repetitive statements needed
(cont’)
(b) find the factorial of 5!
– Without repetition
>> 1*2*3*4*5
– What to do with 1000!?
• A struct for doing such repetitive works is
needed
• Answer: for
for struct
• The for struct
for index = j:k
statements
End
or
for index = j:m:k
statements
end
Important points for struct
• index must be a variable. Each time through the loop it will
contain the next element of the vector j:k or j:m:k, and for each
of these values, statements (which may be one or more
statement) are carried out.
• On completion of the loop the index contains the last value
used.
• If the vector j:k or j:m:k is empty, statements are not executed,
and control passes to the statement following the end
statement.
• The index does not have to appear explicitly in statements. It is
basically a counter.
Example
>> for i = 1:5
x(i)=sqrt(i);
end
>>x
x =
1.0000 1.4142 1.7321 2.0000 2.2361
>> i
i =
5
Sample codes
• Calculating 10!
>> x = 1;
>>for i=2:10,
x=x*i;
end
• Calculating
>> x = 0;
>>for k=1:50,
x=x+k^2;
end
A common mistake: for less loops!
• A very common mistake is to omit the word for
from a for loop.
• Instead of reporting an error, MATLAB obligingly
creates a vector, and executes the statements in
the ‘loop’ only once.
>> x=0
>> i = 1:6,
x=x+i;
end
More on repetition
• for only works when you know when to stop the
loops
– Deterministic
• What to do when you don’t know when to stop.
For example,
– Problem: given a positive fractional number x, find the
smallest integer n that >=x. i.e. ceil(x) provided in
matlab.
– One solution: count 1,2,3,…, stop until the integer
>=x.
In‐deterministic repetition with while
• while struct
while condition
statements
end
• The while construct repeats statements WHILE its
condition remains true
• The condition is tested each time BEFORE
statements are repeated.
• condition must depend on statements in some
way, otherwise the loop will never end.
Programming ceil(x) for x>=0
• Sample code
>> x=5.4;
>> n=0;
>> while ( n<= x)
n=n+1;
end
>>n
n =
6
What to do when you want to finish
loops earlier before its end
• break struct
while condition for i = j:k,
statementsA statementsA
if (condionB), break; end; if (conditionB), break;end;
statementsB statementsB
end end
• During each repetition, after statemensA is
executed, if conditionB holds true, the while
loops will end immediately and go to the next line
after the loop.
Example
• Sample problem: given a positive integer N>2
check whether it is prime or not?
• Solution: for i = 3,…N, check whether the
divison N/i is integer or not?
• The in‐efficiency
– No need to check every integer, as long as there
exist one integer such that the division is integer,
it is not a prime
Code using for struct
>>x=935
>>isprime=0;
>>for i=3:x
r= x/i;
if(round(r)==r),
isprime=0;
end
end
>> isprime
Isprime =
0
>> i
i =
935
Better code using break
>>x=935
>>isprime=1;
>>for i=3:x
r= x/i;
if(round(r)==r),
isprime=0;
break;
end
end
>> isprime
Isprime =
0
>> i
i =
5
What to do when you want execute
part of statements during the loops
• continue struct
while condition for i = j:k,
statementsA statementsA
if (condionB), continue;end; if (conditionB) continue;end
statementsB statementsB
end end
• At each repetition, after statementsA is executed, if
conditionB holds true, statementsB will not be
executed and the program will start the next
repetition.
The difference between break and
continue
• Once conditionB holdes true
– break will finish the whole loop and execute the
line after end
– Continue will finish only the current repetition of
the loop and execute the next repetition of the
loop.