Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: extract every nth element of vector

Tags:

matlab

Is there an easy way to extract every nth element of a vector in MATLAB? Say we have

x = linspace(1,10,10);

Is there a command something like

y = nth(x,3)

so that y = 3 6 9?

Cheers!

like image 221
trolle3000 Avatar asked Oct 25 '11 15:10

trolle3000


People also ask

How do I take the average of every n values in a vector MATLAB?

M = mean( A , 'all' ) computes the mean over all elements of A . This syntax is valid for MATLAB® versions R2018b and later. M = mean( A , dim ) returns the mean along dimension dim . For example, if A is a matrix, then mean(A,2) is a column vector containing the mean of each row.

How do you use the min function in MATLAB?

M = min( A ) returns the minimum elements of an array. If A is a vector, then min(A) returns the minimum of A . If A is a matrix, then min(A) is a row vector containing the minimum value of each column of A .

How do you find the number of elements in an array in MATLAB?

n = numel( A ) returns the number of elements, n , in array A , equivalent to prod(size(A)) .


1 Answers

Try this:

x = linspace(1, 10, 10);
n = 3;
y = x(1 : n : end);  % => 1 4 7 10
y = x(n : n : end);  % => 3 6 9
like image 161
dantswain Avatar answered Dec 07 '22 18:12

dantswain