gpt4 book ai didi

MATLAB:获取 Vector 的等间距条目

转载 作者:太空宇宙 更新时间:2023-11-03 19:52:29 24 4
gpt4 key购买 nike

如何从 MATLAB 中的 Vector 中获取等间距的条目,例如我有以下向量:

 0    25    50    75   100   125   150

当我选择2时,我想得到:

0   150

当我选择3时我想得到:

0   75   150

当我选择4时我想得到:

0   50   100   150

选择 156 应该不起作用,我什至需要检查 if - 条款,但我无法弄清楚。

最佳答案

您可以使用 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

关于MATLAB:获取 Vector 的等间距条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20176787/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com