MATLAB Array Operations, Functions, and Plots · Study

English translation

Topics covered

Calculate distance and velocity from coordinate changes, and check the results using functions and plots.

Calculate an entire array at once

.^, .* , and ./ operate on corresponding elements. * is matrix multiplication. Planar movement lengths can be calculated with sqrt(dx.^2+dy.^2) or hypot(dx,dy).

Function inputs, outputs, and plots

Check input sizes and units and output meanings first, rather than just memorizing function names. Pass time and measurement arrays of equal length to plot, and label axes and units to see whether calculations match the actual changes.

Connect to Onramp

Try the Array Calculations, Calling Functions, and Plotting Data examples, then perform the same calculations on synthetic movement records.

Worked example

Calculate segment lengths and total length for the three movements with dx=[0.30.40]\mathrm{dx}=\left[\begin{array}{ccc}0.3&0.4&0\end{array}\right] m and dy=[0.400.2]\mathrm{dy}=\left[\begin{array}{ccc}0.4&0&0.2\end{array}\right] m.
dx=[.3;.4;0];dy=[.4;0;.2];
segment_m=hypot(dx,dy);total_m=sum(segment_m);
disp(segment_m);disp(total_m);
assert(max(abs(segment_m-[.5;.4;.2]))<1e-12);
assert(abs(total_m-1.1)<1e-12);
bar(segment_m);xlabel('Segment');ylabel('Distance [m]');grid on;

Example results

The segment lengths are .5, .4, and .2 m, totaling 1.1 m.

Official learning materials