0.5 Soft Introduction to MATLAB 35
x1 =
Columns 1 through 10
1 1 1 1 1 1 1 1 1 1
Columns 11 through 15
0 0 0 0 0
A(2:5, 2:5) = zeros(4, 4) % zeros in rows 2−5, columns 2− 5
A =
1 1 1 1 1
1 0 0 0 0
1 0 0 0 0
1 0 0 0 0
1 0 0 0 0
y = rand(1,10) % row vector with 10 random values (uniformly
% distributed in [0,1]
y =
Columns 1 through 6
0.9501 0.2311 0.6068 0.4860 0.8913 0.7621
Columns 7 through 10
0.4565 0.0185 0.8214 0.4447
Notice that these values are between 0 and 1. When using the normal or Gaussian-distributed noise
the values can be positive or negative reals.
y1 = randn(1,10) % row vector with 10 random values
% (Gaussian distribution)
y1 =
Columns 1 through 6
−0.4326 −1.6656 0.1253 0.2877 −1.1465 1.1909
Columns 7 through 10
1.1892 −0.0376 0.3273 0.1746
Using Built-In Functions and Creating Your Own
MATLAB provides a large number of built-in functions. The following script uses some of them.
% using built-in functions
t = 0:0.01:1; % time vector from 0 to 1 with interval of 0.01
x = cos(2∗pi∗t/0.1); % cos processes each of the entries in
% vector t to get the corresponding value in vector x
% plotting the function x
figure(1) % numbers the figure
plot(t, x) % interpolated continuous plot
xlabel(’t (sec)’) % label of x-axis
ylabel(’x(t)’) % label of y-axis