MATLABit
MATLAB stands for MATrix LABoratory. It’s a powerful programming language and software tool created by MathWorks. Its extensive application across engineering, scientific research, academic instruction, and algorithmic design stems from its strengths in numerical computation, data analysis, graphical visualization, and simulation. With a foundation in matrix algebra, MATLAB efficiently manages large datasets and complex mathematical models. Thus, we are well-positioned to commence our exploration of its capabilities. So, let's begin to create vectors in MATLAB.
Table of Contents
Introduction
A vector is defined in MATLAB by enumerating its numerical components inside square brackets [ ]
. The basic form is:
variable_name = [element1 element2 ... elementN]
Row vector
A row vector is created by aligning elements on the same line and separating them with a comma or space.
% using spaces row = [1 2 3 4] % using commas row = [1, 2, 3, 4]
Column vector
To create a column vector, separate elements with semicolons ;
or place each element on a new line inside the brackets.
% using semicolons col = [1; 2; 3; 4] % using new lines col = [ 1 2 3 4 ]
[1,2,3; 4,5,6]
).Other Ways to Create Vectors
Colon operator (start : step : end)
A vector with constant spacing contains elements that increase (or decrease) by the same step. Use the colon operator to specify the start, the step, and the end:
v = start : step : end % start at 'start', step by 'step', stop at or before 'end'
Brackets are optional, so both v = start:step:end
and v = [start:step:end]
are valid.
Examples
% regular spacing of 2
v = 2 : 2 : 10 % produces [2 4 6 8 10]
% default step of 1 (step omitted)
v = 5 : 11 % produces [5 6 7 8 9 10 11]
% descending vector with negative step
v = 10 : -2 : 2 % produces [10 8 6 4 2]
% fractional step
v = 0 : 0.5 : 2 % produces [0 0.5 1.0 1.5 2.0]
end
, MATLAB stops at the last value that does not pass end
(for positive step
the last ≤ end
; for negative step
the last ≥ end
). Floating-point steps can introduce tiny rounding differences — check endpoints when exact values matter.Linearly spaced vectors — linspace
linspace
builds a vector of n elements equally spaced between a specified first value bi
and last value bf
.In order for the first to equal bi
and the last to equal bf
, MATLAB determines the step size.
Syntax
variable_name = linspace(bi, bf, n)
bi — first element, bf — last element, n — number of elements.
Behavior and default
If n
is omitted, MATLAB uses the default n = 100
(so linspace(bi, bf)
returns 100 evenly spaced points from bi
to bf
).
Examples
% five equally spaced values from 1 to 10
v = linspace(1, 10, 5) % produces [1.00 3.25 5.50 7.75 10.00]
% five equally spaced values from 0 to 1
v = linspace(0, 1, 5) % produces [0 0.25 0.5 0.75 1]
% default number of points (100)
v = linspace(0, 1) % produces 100 points from 0 to 1
% identical endpoints produce a constant vector
v = linspace(2, 2, 4) % produces [2 2 2 2]
linspace
guarantees that the first element is bi
and the last is bf
. The step is (bf − bi) / (n − 1)
. For floating-point endpoints, small rounding differences may occur.Applications of Creating Vectors in MATLAB
-
1. Store simple datasets
Keep measurement values or small sample lists as a row vector.
temps = [22.1 23.4 21.9 20.7];
-
2. Indexing & slicing
Access or modify parts of a vector with ranges or strides.
v = 1:10; v(3:5) = 99; sub = v(1:2:end);
-
3. Vectorized arithmetic
Perform element-wise math on entire vectors.
x = 0:0.1:2*pi; y = sin(x) .* exp(-0.1*x);
-
4. Plotting & visualization
Create clean axes with evenly spaced vectors for smooth curves.
t = linspace(0,2*pi,500); plot(t, cos(2*t));
-
5. Grid / mesh generation
Make 2-D domains for surfaces or PDE discretization.
x = linspace(-1,1,200); y = linspace(-2,2,300); [X,Y] = meshgrid(x,y); Z = exp(-(X.^2 + Y.^2)); surf(X,Y,Z)
Conclusion
Creating vectors in MATLAB is a foundational skill that enables efficient data storage, manipulation, and visualization. By mastering vector operations, you can write cleaner, faster, and more flexible code for a wide range of applications — from simple data analysis to complex scientific simulations.
No comments:
Post a Comment