MATLAB Scalar and Matrix Representation

MATLAB (short for matrix laboratory) stores every numeric value as an array of rows and columns. A single pressure reading, a list of forces and a grid of measurements all share one representation; only their dimensions differ. Those dimensions decide how MATLAB indexes, displays and combines your data, and they are the first thing to check when a calculation stops with an error about sizes.

Every value has a size

The array dimensions of a variable are its number of rows mm and columns nn, written m×nm \times n and returned by size. A scalar is not a separate kind of object: it is a 1×11 \times 1 array.


MATLAB
p = 101.3;               % scalar
r = [3 0 -2 5];          % row vector: spaces or commas separate columns
c = [3; 0; -2; 5];       % column vector: semicolons start new rows
K = [4 -1 0; -1 4 -1];   % 2-by-3 matrix
size(K)                  % returns 2 3


Kind

Example

size

numel

length

Scalar

p = 101.3

1 1

1

1

Row vector

r = [3 0 -2 5]

1 4

4

4

Column vector

c = [3; 0; -2; 5]

4 1

4

4

Matrix

K = [4 -1 0; -1 4 -1]

2 3

6

3


Three functions report dimensions, and each answers a different question:

  • size(A) returns the row count and the column count, [m n]; size(A,1) and size(A,2) return one of them.
  • numel(A) returns the total number of elements, mnmn.
  • length(A) returns the larger dimension, max(size(A)), for a nonempty array.

The functions isscalar, isrow and iscolumn test the shape directly and return true or false.



Row versus column orientation

A row vector and a column vector holding the same numbers are different arrays: r is 1×41 \times 4 and c is 4×14 \times 1. Orientation decides how a vector fits beside other arrays, which dimension a function works along, and whether a matrix product is defined. The transpose operator .' turns one orientation into the other, so r.' has size 4 1. The operator ' also transposes, but for complex numbers it takes the complex conjugate at the same time; for real data the two give the same result.


A part extracted from a matrix keeps the shape of that part: K(:,2) is the second column, size 2 1, and K(1,:) is the first row, size 1 3.

Column-major storage and linear indexing

You usually address an element by its row and column subscripts, A(i,j). In memory, MATLAB lays the elements out in column-major order: all of column 1 from top to bottom, then all of column 2, and so on. Every element therefore also has a single linear index kk, and A(k) refers to the same element as A(i,j). For an array with mm rows,


k=(j−1) m+ik = (j - 1)\,m + i


The term (j−1)m(j-1)m counts the elements in the full columns to the left, and ii counts down the current column.


A 3-by-4 grid of matrix cells, each labelled with its row and column subscripts and its linear index, with arrows running down each column.


For the 3×43 \times 4 array in the figure, element (2,3)(2,3) has k=(3−1)(3)+2=8k = (3-1)(3) + 2 = 8. Going the other way, the column is j=⌈k/m⌉j = \lceil k/m \rceil and the row is i=k−(j−1)mi = k - (j-1)m. The functions sub2ind and ind2sub perform both conversions:


MATLAB
A = zeros(3,4);
k = sub2ind(size(A), 2, 3)      % k = 8
[i, j] = ind2sub(size(A), 8)    % i = 2, j = 3



Each real double element occupies 8 bytes, so the data of an m×nm \times n double matrix take 8mn8mn bytes.


What to remember

  • A scalar is a 1×11 \times 1 array, a row vector is 1×n1 \times n, a column vector is m×1m \times 1.
  • size gives [m n], numel gives mnmn, length gives the larger dimension.
  • Orientation is part of the data; .' converts a row to a column and back.
  • Elements are stored in column-major order, with linear index k=(j−1)m+ik = (j-1)m + i.

Example: Sizing and indexing a sensor log

A building monitoring system records the temperature at 5 sensors once per hour for 24 hours. The readings are loaded into MATLAB as a double matrix T with one row per hour (hours numbered 1 to 24) and one column per sensor (sensors numbered 1 to 5).


a) Find size(T), numel(T) and length(T), and the memory, in bytes, that the readings occupy.


One row per hour and one column per sensor gives m=24m = 24 rows and n=5n = 5 columns, so size(T) returns 24 5.


The element count is mn=24×5=120mn = 24 \times 5 = 120, so numel(T) returns 120. The function length returns the larger dimension, max⁡(24,5)=24\max(24, 5) = 24, so length(T) returns 24, not 120.


Each real double occupies ________ bytes:


memory=120×8 bytes=960 bytes\text{memory} = 120 \times 8\ \text{bytes} = 960\ \text{bytes}


b) Give the sizes of s3 = T(:,3), the full record from sensor 3, and h10 = T(10,:), all sensor readings at hour 10.


T(:,3) takes every row of column 3. The result keeps the column shape, 24 rows by 1 column, so size(s3) is 24 1 and s3 is a column vector.


T(10,:) takes every column of row 10, giving 1 row by 5 columns, so size(h10) is 1 5 and h10 is a row vector. If you later need the hour-10 readings as a column, h10.' has size 5 1.


c) The reading for hour 10, sensor 3 is T(10,3). Find the linear index k such that T(k) is the same element.


MATLAB stores T in column-major order with m=24m = 24 rows, so


k=(j−1) m+i=(3−1)(24)+10=48+10=58k = (j-1)\,m + i = (3-1)(24) + 10 = 48 + 10 = 58


Check by counting: columns 1 and 2 hold 2×24=482 \times 24 = 48 elements, and the 10th element of column 3 comes next as number 58. In MATLAB, sub2ind(size(T), 10, 3) returns 58.


d) A colleague's script reads T(100). Which hour and which sensor does that value come from?


Find the column first. Each column holds ________ elements, so


j=⌈10024⌉=⌈4.17⌉=5j = \left\lceil \frac{100}{24} \right\rceil = \lceil 4.17 \rceil = 5


The first four columns contain 4×24=964 \times 24 = 96 elements, so the row is


i=100−96=4i = 100 - 96 = 4


T(100) is T(4,5): the reading at hour 4 from sensor 5. Check: (5−1)(24)+4=100(5-1)(24) + 4 = 100, and [i, j] = ind2sub(size(T), 100) returns i = 4, j = 5.


A row-by-row count, 100=(i−1)(5)+j100 = (i-1)(5) + j, would give T(20,5), hour 20. That is the classic mistake of borrowing a row-major formula; MATLAB counts down columns.

After running


MATLAB
v = [2; 7; -1; 4];
d = size(v);


what does d contain?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An engineering program stores one calibration temperature and a sequence of twelve temperature readings. Complete the statements using the MATLAB array terms scalar and vector.


The single calibration temperature, stored in a one-by-one numeric array, is a . The twelve readings, stored together in one row, form a , even though temperature has no physical direction.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A student stores a grid of results in


MATLAB
M = zeros(6, 9);
L = length(M);


What value does L hold?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors