MATLAB:获得Vector的等距条目

tim*_*tim 4 matlab space vector distance

怎么可能从MATLAB中的Vector获得等距条目,例如我有以下向量:

 0    25    50    75   100   125   150
Run Code Online (Sandbox Code Playgroud)

当我选择时2我想得到:

0   150
Run Code Online (Sandbox Code Playgroud)

当我选择时3我想得到:

0   75   150
Run Code Online (Sandbox Code Playgroud)

当我选择时4我想得到:

0   50   100   150
Run Code Online (Sandbox Code Playgroud)

选择1,5或者6不应该工作,我甚至需要检查if一下 - 但是我无法解决这个问题.

Lui*_*ndo 5

您可以使用linspace和生成索引round:

vector = [0    25    50    75   100   125   150]; % // data
n = 4; % // desired number of equally spaced entries

ind = round(linspace(1,length(vector),n)); %// get rounded equally spaced indices
result = vector(ind) % // apply indices to the data vector
Run Code Online (Sandbox Code Playgroud)

如果要强制值1,5或6 n不起作用:test if n-1divides length(vector)-1).如果你这样做,你不需要round获得索引:

if rem((length(vector)-1)/(n-1), 1) ~= 0
    error('Value of n not allowed')
end
ind = linspace(1,length(vector),n); %// get equally spaced indices
result = vector(ind) % // apply indices to the data vector
Run Code Online (Sandbox Code Playgroud)