繁体   English   中英

MATLAB:获得Vector的等距条目

[英]MATLAB: get equally spaced entries of Vector

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

 0    25    50    75   100   125   150

当我选择2我想得到:

0   150

当我选择3我想得到:

0   75   150

当我选择4我想得到:

0   50   100   150

艇员选拔156不应该工作,我甚至需要的支票的if -clause为,但我不知道这一点。

您可以使用linspaceround生成索引:

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

如果你想强制n值为1,5或6不起作用:测试n-1除以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

使用linspace

>> a
a =

     0    25    50    75   100   125   150

>> a(linspace(1,length(a),4))
ans =

     0    50   100   150

>> a(linspace(1,length(a),3))
ans =

     0    75   150

>> a(linspace(1,length(a),2))
ans =

     0   150

请注意,除1之外,无效值会引发错误:

>> a(linspace(1,length(a),5))
error: subscript indices must be either positive integers or logicals
>> a(linspace(1,length(a),6))
error: subscript indices must be either positive integers or logicals

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM