Sie sind auf Seite 1von 8

New commands and functions

• ones
» X = ones; returns the scalar 1.
» X = ones(n); returns an n-by-n matrix of ones.
» X = ones(sz1,...,szN); returns an sz1-by-...-by-szN
array of ones where sz1,...,szN indicates the size of each
dimension. For example, ones(2,3) returns a 2-by-3array of ones.
» X = ones(sz); returns an array of ones where the size
vector, sz, defines size(X).For example, ones([2,3]) returns a 2-by-3
array of ones.
• zeros
» X = zeros; returns the scalar 0.
» X = zeros(n); returns an n-by-n matrix of zeros.
» X = zeros(sz1,...,szN); returns an sz1-by-...-by-szN
array of zeros where sz1,...,szN indicates the size of each
dimension. For example, zeros(2,3) returns a 2-by-3array of zeros.
» X = zeros(sz); returns an array of ones where the size
vector, sz, defines size(X). For example, zeros([2,3]) returns a 2-by-3
array of zeros.
(Dot). Operator: To indicate an array (element-by-element) operation,
precede a standard operator with a period (dot).
Examples:
>> x = [ 1 2 3 ];
>> y = [ 4 5 6 ];
>> a = x .* y
a =
4 10 18
>> b = x .\ y
a =
4.0000 2.5000 2.0000
>> c = x ./ y
c =
0.2500 0.4000 0.5000
>> d = x .^ y
d =
1 32 729
>> a = zeros(3) >> d = ones([3 3 3])
a = d(:,:,1) =
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1
d(:,:,2) =
>> b = ones(2,3) 1 1 1
b = 1 1 1
1 1 1 1 1 1
1 1 1 d(:,:,3) =
1 1 1
>> c = ones(2,3) * 2 1 1 1
c = 1 1 1
2 2 2
2 2 2

>> d = ones([3 2])


d =
1 1
1 1
1 1
>> x = [ 2:2:8 ]
x =
2 4 6 8

>> y = [3,5:7]
y =
3 5 6 7

>> x * y
Error using *
Inner matrix dimensions must agree.

>> x * y'
a =
118

>> x .* y
a =
6 20 36 56

>> x' .* y ;
Error using .*
Matrix dimensions must agree.
>> z = 7;
>> a = x * z x= [2 4 6 8]
a = 14 28 42 56 y= [3 5 6 7]

>> b = x .* z
b =
14 28 42 56

>> c = x *. z
c = x *. z
|
Error: Unexpected MATLAB operator.
>> d = x ./ y
d =
0.6667 0.8000 1.0000 1.1429

>> r = x .\ y
r =
1.5000 1.2500 1.0000 0.8750

>> f = x .^ y
f =
8 1024 46656 2097152
x= [2 4 6 8]
>> g = x .^ .5
y= [3 5 6 7]
g =
z=7
1.4142 2.0000 2.4495 2.8284

>> h = x ^ z
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.

>> i = x .^ z
i =
128 16384 279936 2097152

>> h = ones( x(1 ,2) )


h =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
>> i = x .^ y(1)
i =
8 64 216 512
>> j = ones(2,5) * z
j = x= [2 4 6 8]
7 7 7 7 7 y= [3 5 6 7]
7 7 7 7 7 z=7

>> k = ones(2,4) + [ y ; x ] * 10
k =
31 51 61 71
21 41 61 81
Exercises: find the value of x in the following equations
10 10
1 𝑖 2𝑖
𝑥=෍ 2 𝑥=෍
𝑖 𝑖+3
𝑖=1 𝑖=1

>> x = sum(1 ./ [1:10].^2)


>> i=[1:10];
>> a=2*i./(i+3);
x =
>> sum( a.^(1./i))
1.5498
ans =
−9
𝑖 9.7104
𝑥 = 100 ∗ ෑ
𝑖 + 12
𝑖=−1

>> i = [-1:-1:-9];
>> x = prod( abs(i)./(i+12) )*100
x =
1.82

Das könnte Ihnen auch gefallen