INTRODUCTION
TO MATRIX
PROGRAMMING
Calvin Kuo
BMEG 257, CLASS 15, 03/04/2025
CLASS OUTLINE
• Matrix programming
• Overview of computer programming
• Practical programming for data analysis
• How to represent data in code
2
PROGRAMMING LANGUAGES
• Why do we need so many languages???
• Which one should I use?
• Common languages in SBME
3
PROGRAMMING LANGUAGES
• Why do we need so many languages???
• Which one should I use?
• Common languages in SBME
4
POPULAR LANGUAGES AND THEIR PROS / CONS
Python Java
• Scripting language • Compile language
• Easy to write • Packaged for transportability
• Easy to run • Bulky applications
• Slow and inefficient • Less control for optimization
C/C++
Matlab, R
• Compile language
• Scripting language
• Offers full control for
• Optimized specifically for
optimization / speed
numerical computations
• Difficult to master
5
DO YOU NEED TO ONLY USE ONE LANGUAGE?
Sensor Firmware: C
Maximum memory and speed
efficiency
App Backend App Frontend
Prototype: Matlab Prototype: Python
Easy to implement and Easy to implement and
run data analysis tools run
App Final: C++
Efficient and optimized code 6
PRACTICAL PROGRAMMING
Purpose of this module is to teach you how to
practically use programming tools for data
analysis, NOT to teach you how to program
7
WHAT IS DATA?
Descriptive
Numerical
DATA = INFORMATION
Images and Videos
8
REPRESENTING DATA IN CODE
• In most cases, data is represented in matrix form
• What is a matrix? rows
3 columns
1 2 3
𝐴𝐴 = 2 rows 𝐴𝐴 is a 2 × 3 matrix
4 5 6
columns
• Generalizes to fewer dimensions
• 1-dimension matrix is a vector
• 0-dimension matrix is a scalar
• Generalizes to more dimensions
• 𝑛𝑛1 × 𝑛𝑛2 × 𝑛𝑛3 × ⋯ with each 𝑛𝑛𝑖𝑖 referring to the number of elements
in dimension 𝑖𝑖 9
EXAMPLE: TIME SERIES DATA
• Consider a thermal sensor
• Continuous measure of temperature
• How do we read temperatures?
• We look at (sample) temperatures every
time we look at the thermometer Time Temp
• Similar process for electronic sensors
0:00:00 - 25°
• Takes a reading from the thermal sensor 0:01:00 - 24°
0:02:00 - 25°
at specific times 0:03:00 - 25° n samples
• Continuous signal (analog) to discrete 0:04:00 - 24°
0:05:00 - 27°
samples (digital) ⋮ ⋮
10
EXAMPLE: TIME SERIES DATA
• What if we want to make other
measurements as well?
Option 1 Option 2
Simultaneous Measurement Separate Matrices
Time Temp 1 Pressure
0:00:00 - 25° - 1.0atm 0:00:00 - 25° 0:00:00 – 1.0atm
0:01:00 - 24° - 1.2atm 0:01:00 - 24° 0:01:30 – 1.2atm
0:02:00 - 25° - 1.0atm 0:02:00 - 25° 0:03:00 – 1.0atm
0:03:00 - 25° - 1.0atm n samples n samples 0:03:00 - 25° 0:04:30 – 1.0atm m samples
0:04:00 - 24° - 1.1atm 0:04:00 - 24° 0:06:00 – 1.1atm
0:05:00 - 27° - 1.1atm 0:05:00 - 27° 0:07:30 – 1.1atm
⋮ ⋮ ⋮ ⋮ ⋮ ⋮ 11
EXAMPLE: SURVEY DATA
• Surveys have a variety of data types, Sample questions from
not all quantitative SCAT3 Survey
• Numerical 1. Name: 3. Number of
• Yes / no Symptoms:
2. Age:
• Multiple choice options
• Ranking / scale 4. Frequency of
headaches: Never Rarely Sometimes Often
• Open-ended
• Need a consistent way to store data 5. How different No Different N/A
is athlete acting? Very Different Unsure
• Assign numerical values where
possible 6. Neck examination
findings: 12
EXAMPLE: SURVEY DATA
• Open-ended: Assign a unique Sample questions from
numerical identifier per name SCAT3 Survey
• Numerical: Already numerical 1. Name: 3. Number of
• Ranking / Scale: Assign number to Symptoms:
2. Age:
each option, can be related to
“intensity” 4. Frequency of
headaches: Never Rarely Sometimes Often
• Multiple choice options: Assign
number to each option (arbitrary) 5. How different No Different N/A
is athlete acting? Very Different Unsure
6. Neck examination
findings: 13
EXAMPLE: SURVEY DATA
• What to do about open-ended Sample questions from
questions? SCAT3 Survey
• Perform analyses to convert to 1. Name: 3. Number of
quantitative Symptoms:
2. Age:
• Semantic analysis
4. Frequency of
# non-injury # injury headaches: Never Rarely Sometimes Often
keywords keywords
5. How different No Different N/A
• Normal • Stiff is athlete acting? Very Different Unsure
• Unchanged • Painful
• Uninjured • Bruise 6. Neck examination
• Fine • Sore findings: 14
EXAMPLE: SURVEY DATA
Q1 Q2 Q3 Q4 Q5 Q6
Sample questions from
1, 31, 0, 1, 1, 0 SCAT3 Survey
2, 27, 0, 1, 2, 0
3, 25, 0, 1, 1, 0 1. Name: 3. Number of
4, 40, 1, 1, 2, 2
5, 42, 5, 4, 3, 1 2. Age: Symptoms:
6, 21, 2, 2, 1, 0
5, 42, 4, 4, 1, 0
5, 42, 5, 4, 1, 0
4. Frequency of
6, 21, 1, 1, 1, 0 n surveys headaches: Never Rarely Sometimes Often
4, 40, 0, 1, 1, 2
1, 31, 0, 1, 1, 0 5. How different No Different N/A
⋮ ⋮ ⋮ ⋮ ⋮ ⋮ is athlete acting? Very Different Unsure
6. Neck examination
findings: 15
EXAMPLE: IMAGES
• How do we represent images as a matrix?
• Hint: What does the resolution represent?
• Hint: What are primary colors?
• Images have many
individual pixels
• Number of pixels and
organization defined by
resolution
• Each pixel has a color
• Color represented by
Red = 0.8 intensity in the primary Red,
Green = 0.2
Blue = 0.0
Green, Blue channels 16
Caveats: Image resolutions do not always follow same
EXAMPLE: IMAGES row x column convention as matrices.
Caveat: Pixel location in a matrix does not always
correspond to location in the image
Image resolution is
316 x 356 pixels Color Channel 3x colors
316 pixels
0.4, 0.4, 0.3, 0.5, 0.9,
0.2, 0.3, 0.5, 0.8, 0.9, …
0.3, 0.1, 0.4, 0.8, 0.8
0.2, 0.2, 0.8, 0.8, 0.7
⋮ ⋱
356 pixels
356 x 316 x 3 matrix
17
Videos can be
EXAMPLE: IMAGES represented by 4-
dimensional matrices!
• How does this extend to videos?
• Videos are just a series of still images, so… 356 x 316 x 3 x # frames
Frame 1 Frame 2 Frame n
… …
356 x 316 x 3 matrix 356 x 316 x 3 matrix 356 x 316 x 3 matrix
18
WHY STORE DATA LIKE THIS?
• Convenient representation for data access and analytics
• Accessing data
What we want Matlab Syntax Python Syntax
Create a 1 2 3
𝐴𝐴 = A = [1, 2, 3; 4, 5, 6] A = numpy.array([[1, 2, 3], [4, 5, 6]])
matrix 4 5 6
Access the top topleft = A(1,1) topleft = A[0,0]
left element
Access the midcol = A(:,2) midcol = A[:,1]
middle column midcol = A(1:2,2) midcol = A[0:2,1]
19
Frame 1 Frame 2 Frame n
WHY STORE DATA LIKE THIS? … …
356 x 316 x 3 356 x 316 x 3 356 x 316 x 3
• Example of accessing data in video matrix matrix matrix
• Recall video data can be stored as a 4-dimensional matrix
• V = 356 x 316 x 3 x # frames matrix
• What does each dimension represent?
Access image Access the “video” of Access green channel
in frame 35 the upper left quadrant between frames 5-15
image = V( :, :, :, 35) image = V( 1:178, 1:158, :, :) image = V( :, :, 2, 5:15)
20
CLASS RECAP
What did we cover? Reminders
• Representing data as matrices • Circuits assignment due Friday –
• Time series data review lecture 13 and 14!
• Images and videos
Next time
• How do we store this data?
21
THANKS!