![](/img/trans.png)
[英]Matlab: Classify random vector w.r.t. equally spaced vector of different size
[英]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
艇员选拔1
, 5
或6
不应该工作,我甚至需要的支票的if
-clause为,但我不知道这一点。
您可以使用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
如果你想强制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.