I have two vectors in the form of columns, e.g.:
a = 1
2
3
4
5
b = 2
1
3
5
4
I am using the following code to retrieve the product of each:
for i = 1 : length(a)
ab(i) = a(i) * b(i);
end
This gives:
ab = 2
2
9
20
20
This is fine, and it produces the correct answer, but it seems slightly inefficient; I presume there must be a syntactical way of doing this without the ‘for’ loop?
Try
a.*bnotice the
.before the*which tells Matlab to do the multiplication element by element.