Introduction to MATLAB
Defining matrices via the vector technique
Using the for loop in MATLAB is relatively expensive. It is much more efficient to perform the same task using the vector method. For example, the following task
for j=1:n
for i=1:m
A(i,j) = B(i,j) + C(i,j);
end
end
can be more compactly and efficiently represented (and computed) by the vector method as follows:
A(1:m,1:n) = B(1:m,1:n) + C(1:m,1:n);
If the matrices are all of the same size (as is the case here), then the above can be more succinctly written as
A = B + C;
For sufficiently large matrix operations, this latter method is vastly superior in performance. More examples ...
|