Matlab:索引和相关值
首先,我的编程技能不是很好,所以请容忍我 我试图在Matlab R2015a中编写以下脚本: 1) 给定某个向量(Matlab:索引和相关值,matlab,indexing,Matlab,Indexing,首先,我的编程技能不是很好,所以请容忍我 我试图在Matlab R2015a中编写以下脚本: 1) 给定某个向量(mag),从该向量的前300个值(Am和a)计算两个值得注意的值 2) 获取Am值的索引,以及A值的两个索引(向量是对称的,因此A将有两个索引) 3) 从另一个向量(freq)获取与前面三个索引(分别来自Am、第一个A和第二个A)关联的值(fm、f1和f2) 4) 最后,根据3)中的值计算D 到目前为止,我得到的是: Am=max(mag(1:300)); %Am is the
mag
),从该向量的前300个值(Am
和a
)计算两个值得注意的值
2) 获取Am
值的索引,以及A
值的两个索引(向量是对称的,因此A
将有两个索引)
3) 从另一个向量(freq
)获取与前面三个索引(分别来自Am
、第一个A
和第二个A
)关联的值(fm
、f1
和f2
)
4) 最后,根据3)中的值计算D
到目前为止,我得到的是:
Am=max(mag(1:300)); %Am is the maximum value of vector mag
A=Am/2^0.5; %A is the other desired value
[~,Im] = mag(1:300,Am); %Trying to get the Am index. Error: "Indexing cannot yield multiple results." I found that this error is usual when using variables with the same name, which is not the case.
fm=freq(Im); %Value of freq associated with Am
[~,I1] = mag(1:300,A,'first'); %Index of the first value of A
f1=freq(I1) ; %Value of freq associated with the first value of A
[~,I2] = mag(1:300,A,'second'); %Index of the second value of A
f2=freq(I1); %Value of freq associated with the second value of A
D=(f2^2-f1^2)/(4*fm)
我无法从mag
的期望值中获取相关的freq
值。欢迎提供任何提示和建议
提前谢谢你 以下是一些建议:
%find the index of a certain value
l1=find(mag(1:300)==Am);
% find the first index
l2=find(mag(1:300)==Am, 'first');
通常,如果你有一个带索引的向量,那么你可以用它来得到另一个向量的值,比如
a=[1 3 6 8];
b=rand(10,1);
b(a) % yields the first, third, sixth and eighth value of b
请记住,索引向量必须是整数类型,否则它将无法工作。
你可以发布准确的错误信息,也可以举一个mag的例子吗
希望这有助于发现我可以用[~,I1]=max(mag(1:300))替换[~,Im]=mag(1:300),效果很好。对于其他两个索引,问题仍然存在。在第一段代码中,您将
mag
用作变量,进一步用作函数。这里没有什么真正的意义。@thewaywewalk,如果您试图从A的两个值中获取索引,请忽略其中的内容。我似乎找不到一个方法来做这件事,这是一个错误的尝试,我的错误。“使用find Second参数的错误必须是一个正的标量整数。”,当使用l2=find(mag(1:300)=Am,‘first’);mag是一个3908x1双精度,是快速傅里叶变换的abs()。此外,是否可以将“first”替换为“second”,即获取第二个索引?l2=find(mag(1:300)=Am,1,“first”);