Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Matlab Syntax (1:end-1) - Explanation [closed]

Writer Matthew Martinez
$\begingroup$

Could someone explain me this code?

meas.jerk.time = (meas.acc.time(1:end-1) + meas.acc.time(2:end)) ./ 2;

Assuming that meas.acc.data and meas.acc.time are vectors with the same number of elements, then diff on the data vector will return a numeric vector with one element fewer than the time vector, so meas.jerk.data and meas.jerk.time will likely have mismatching sizes.

But idk the syntax of this code. What does (1:end-1) mean or (2:end) ./2?

$\endgroup$

1 Answer

$\begingroup$

Matlab has a lot of array indexing tools. If v is a vector, then v(1:end-1) gives you the first through the second last elements of v, and v(2:end) gives the second through the last elements.

For more information on this, see the help page: .

Furthermore, the ./operator stands for "element-wise right division" --, so v ./ 2 will divide every element of v by 2.

$\endgroup$