Matlab:索引和相关值

时间:2015-04-27 10:10:03

标签: matlab indexing

首先,我的编程技巧不是很好,所以请耐心等待。

我试图在Matlab,R2015a中编写以下内容:

1)给定某个向量(mag),从该向量的前300个值(AmA)计算两个值得注意的值;

2)获取Am的值的索引,以及值A的两个索引(向量是对称的,因此A将有两个索引);

3)获取与前三个索引关联的值(fmf1f2)(来自Am的索引,第一个A和第二个索引来自另一个向量(A)的<{1}}。

4)最后,根据3)中的值计算freq

到目前为止,这就是我所拥有的:

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) 的欲望值中获取相关的freq值。任何提示和建议都非常受欢迎。

提前谢谢你!

1 个答案:

答案 0 :(得分:0)

这里有一些建议:

%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的一个例子?

希望这会有所帮助

相关问题