Saltar al contenido
Hub Nexus
Actualizado

AutorAún sin autorAsumirla

¿Ves algo que mejorar? Propón un cambio.

Apoyo

Esta página es pública.

MATLAB is a vector programming language. The most efficient use of MATLAB will involve taking advantage of the built-in capabilities for manipulating data instead of using constructs such as loops.

Basic Math

Most arithmetic operators will work as expected on vectors:

>> a = [2 43 943 78];
>> 5 * a
ans =
10 215 4715 390
>> a / 2
ans =
1.0000 21.5000 471.5000 39.0000
>> 0.2 + a
ans =
2.2000 43.2000 943.2000 78.2000

Likewise, all of these operations can be done with matrices for the expected results.

Most MATLAB functions, such as sin or log, will return a vector or matrix of the same dimensions as its input. So to compute the sine of all the integers between 0 and 10, it suffices to run

>> sin(0:10)

and the returned vector will contains ten values.

Per Element Operations

Operators such as the carrot (^) or multiplication between vectors may not work as expected because MATLAB sees vectors the same as any other matrix, and so performs matrix power and matrix multiplication. All operators can be prefixed by a . to make it explicit that an operation should be performed on each element of the matrix. For example, to compute the differences of the sine and cosine function squared for all integers between 1 and 4, one can use:

>> (sin(1:4) - cos(1:4)).^2
ans =
0.0907 1.7568 1.2794 0.0106

as opposed to

>> (sin(1:4) - cos(1:4))^2
??? Error using ==> mpower
Matrix must be square.

which results from MATLAB's attempt to square a 1x4 vector using matrix multiplication.

Using .* or ./ allows one to divide each element of a matrix or vector by the elements of another matrix or vector. To do this, both vectors must be of the same size.

Converting Loops to Vector-based mathematics

Since MATLAB is a vector language, an artificial algorithm such as

x = [];
v = [5,2,4,6];
for i=1:4
x(i) = v(i) * ((i+32)/2 - sin(pi/2*i));
if(x(i) < 0)
x(i) = x(i) + 3;
end
end

can be done far more efficiently in MATLAB by working with vectors instead of loops:

i = 1:4;
v = [5,2,4,6];
x = v .* ((i+32)/2 - sin(pi/2*i));
x(x<0) = x(x<0) + 3;

Internally, MATLAB is of course looping through the vectors, but it is at a lower level then possible in the MATLAB programming language.

Where this page came from

This page was imported from Wikibooks. From “MATLAB Programming” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

En estas publicacionesMATLAB Programming

IdiomasEnglish

Licencia: CC BY-SA 4.0 · Adaptado de en.wikibooks.org

Puntos de vista

Comentarios

Spinner Logo
Versión: 2CC0 1.0 (dominio público)
The runaway star that left the Tarantula Nebula
Versión: 2CC0 1.0 (dominio público)
The Eagle Nebula, seen in the infrared
Versión: 2CC0 1.0 (dominio público)
The Cosmic Cliffs are not cliffs
Versión: 2CC0 1.0 (dominio público)
The recommended targets are real, but the largest health gain is between doing nothing and doing a little. That is the part worth knowing if you are starting from nothing.
Versión: 2CC0 1.0 (dominio público)
Sleep is not one state but a cycle through several, each doing different work. Most sleep disorders are a failure to complete that cycle rather than a shortage of hours.
Versión: 2CC0 1.0 (dominio público)
Four nebulae that revealed the shape of our galaxy