What does the syntax matrix(vector) mean?

1 view (last 30 days)
Raven
Raven on 17 Jul 2024
Edited: Voss on 17 Jul 2024
I'm reviewing some code written by someone else, and it includes a line of the format:
a = m(v)
where m is a matrix and v is a vector. It's unclear to me exactly what operation this is performing. At first, I thought it was matrix multiplication, but the matrix does not have the proper dimensions to operate on the vector. The matrix is 1006x321, and the vector is 143x1. The output of this operation, a, is also a 143x1 vector. Since it can't be proper matrix multiplication, I really don't know what operation is being performed here. What exactly does this syntax mean?

Accepted Answer

Voss
Voss on 17 Jul 2024
  2 Comments
Raven
Raven on 17 Jul 2024
Ohh, ok, I didn't really think this was it, as I didn't know you could index matrices with one value, and the values in the vector were quite large, but it makes sense given the way it works. Thanks!
Voss
Voss on 17 Jul 2024
Edited: Voss on 17 Jul 2024
You're welcome!
Here's an example to illustrate indexing a matrix with a vector:
m = reshape(100+(1:27),[],3)
m = 9x3
101 110 119 102 111 120 103 112 121 104 113 122 105 114 123 106 115 124 107 116 125 108 117 126 109 118 127
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
v = 1:2:15 % a vector containing the odd numbers from 1 to 15
v = 1x8
1 3 5 7 9 11 13 15
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% a will contain the 1st, 3rd, 5th, ..., 15th elements of m.
% note that the counting goes down the first column of m first, then
% continues down the second column, and so on.
a = m(v)
a = 1x8
101 103 105 107 109 111 113 115
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Sign in to comment.

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!