Creating Arrays
MATLAB Array Construction
MATLAB array construction lets you turn measurements, sampling locations, and initial values into organized numeric data. Before typing a command, decide what the entries represent and how many rows and columns you need. You can enter irregular measurements explicitly, generate regularly spaced values, or create an array filled with zeros or ones. These choices control both the contents and the arrangement of the resulting array.
Enter values and join blocks
Use square brackets to enter numbers directly. Within the brackets, spaces or commas separate entries across a row, while semicolons separate rows:
r = [6 9 12];
c = [6; 9; 12];
A = [6 9; 12 15];Here, r has one row and three columns, c has three rows and one column, and A has two rows and two columns. Every row of an ordinary numeric matrix needs the same number of entries so that the matrix remains rectangular.
Notice the two roles of a semicolon: inside brackets, it separates rows; at the end of a statement, it suppresses displayed output. Suppressing output does not change the stored array.
Concatenation means joining existing arrays to construct a new array. Write [A B] to place B to the right of A, or [A; B] to place B below A. Horizontal joining requires equal row counts; vertical joining requires equal column counts.
Generate evenly spaced values
For a sequence with a specified increment, use start:step:stop:
x = 2:3:11;This creates the row vector [2 5 8 11]. The middle number is the increment between entries. If you omit it, MATLAB uses an increment of positive one. Use a negative increment to generate descending values.
The stopping value is a boundary, so it is included only when the increment reaches it, subject to floating-point effects. For example, 2:3:12 also produces [2 5 8 11]; MATLAB does not append 12 using a shorter final interval. An increment pointing away from the stopping value produces an empty sequence, as in 7:1:3.
Use linspace(a,b,n) when you know the endpoints and the required number of values. For an integer n≥2, both endpoints are included. The spacing is the difference between adjacent values:
There are n−1 intervals between n values. Thus, linspace(1,4,7) creates [1 1.5 2 2.5 3 3.5 4], with spacing (4−1)/(7−1)=0.5.
Requirement | Construction | What you specify |
|---|---|---|
Known individual entries | [6 9 12] | Each value |
Known increment | 2:3:11 | Start, step, boundary |
Known point count | linspace(1,4,7) | Start, end, count |
Create filled arrays and check dimensions
Use zeros(m,n) to create an array with m rows and n columns containing only zeros. Similarly, ones(m,n) fills that shape with ones:
Z = zeros(3,2);
U = ones(1,4);Z contains six zeros arranged in three rows and two columns. U contains four ones in a single row. With one scalar size argument, zeros(4) creates a 4-by-4 square array; zeros(1,4) creates a row vector.
Use size(Z) to check its dimensions; the result is [3 2]. Keep physical units in your description or variable naming: an ordinary numeric array does not automatically attach metres or seconds to its entries.
What to remember
- Brackets organize entries; spaces separate columns and semicolons separate rows.
- Concatenation requires matching dimensions across the joining boundary.
- The colon operator controls the increment; linspace controls the point count.
- For filled arrays, specify rows first and columns second.
Example: Construct pressure-test arrays
Two pressure sensors are sampled simultaneously from 0 to 2.0 s at intervals of 0.5 s, including both endpoints. Sensor 1 records [102 105 108 110 111] kPa, and sensor 2 records [99 101 104 107 109] kPa. Store sensor measurements in rows, with columns following the sampling times.
a) Create the sampling-time row vector t using the colon operator. State its values and dimensions.
The starting time is 0 s, the increment is ________, and the stopping time is ________. Put these values into the start:step:stop pattern:
t = 0:0.5:2.0;The number of intervals is:
Including the starting point gives 4+1=5 sampling times. The stored row vector is [0 0.5 1.0 1.5 2.0], with entries representing seconds. Its dimensions are 1-by-5.
b) Create separate row vectors p1 and p2, then construct the pressure array P with sensor 1 above sensor 2. State the completed array and dimensions.
Enter each sensor's five measurements across a row. Then use a semicolon inside brackets to join the rows vertically:
p1 = [102 105 108 110 111];
p2 = [99 101 104 107 109];
P = [p1; p2];Both inputs have ________ columns, so their column counts match. Their row counts add to 1+1=2, giving:
Every entry represents pressure in kPa. The completed array is 2-by-5, containing 2×5=10 measurements. Each column corresponds to the same sampling time for both sensors.
c) Create a separate zero-filled array R with space for the same two sensors and five times in a future test. State its contents and explain what its zeros mean.
The storage arrangement needs two rows and five columns, so specify both dimensions explicitly:
R = zeros(2,5);This produces:
The array contains 2×5=10 zeros and has dimensions 2-by-5, matching P. These zeros are initial storage values for future readings in kPa; they do not establish that the sensors measured zero pressure. Specifying both dimensions also avoids accidentally creating a square array.
An engineering simulation needs a 5-by-5 array stored in full (nonsparse) format, with ones on the main diagonal and zeros everywhere else. Complete the command by supplying one built-in MATLAB function name.
A = function_name(5);Replace function_name with .
An engineer creates descending speed settings in m/s using the MATLAB command v = 18:-4:1;. Which row vector is stored in v?