我有一个依赖于另一个的数组(即我有时间数组函数的张力(V)数组)。我想在特定时刻找到张力阵列元素的位置(即对应于时间向量的特定位置)。我怎样才能做到这一点?
我尝试了以下内容:
VMpzt = max(V);
%find the maximum value of the array of tensions
idxV = find(V == VMpzt);
%find the corresponding index
idxt = t(idxV);
%find the corresponding index of time vector
diff = 0.0048377328;
idxt2 = idxt-diff;
%in fact, I am interested at the index of time that corresponds to this particular position
Vmpzt = V(idxt2); %find the corresponding position of V array
这样做,我收到了错误
“数组索引必须是正整数或逻辑值。”
答案 0 :(得分:0)
idxt = t(idxV); %find the corresponding index of time vector
此行不符合您的评论建议。它找到该指数的时间,而不是指数。然后从中减去diff
并尝试将其用作索引,但它不是索引,而是时间。我认为你的变量名混淆了你。试试这个:
[V_max, V_max_index] = max(V);
t_at_v_max = t(V_max_index); %find the TIME where V was max
diff = 0.0048377328;
t_desired = t_at_v_max - diff;
您现在希望t
等于t_desired
的索引。因为您正在处理floating point numbers,所以不能只执行find(t==t_desired)
,所以我们需要这样做:
t_desired_index = find(abs(t-t_desired) < diff/1000); %1000 chosen arbitrarily, you might need to change it
最后:
Vmpzt = V(t_desired_index );