2015-04-27 134 views
-2

首先,我的编程技能不是很好,所以请耐心等待。Matlab:索引和相关值

我试图脚本在Matlab以下,R2015a:

1)给定一个特定向量(mag),从该载体(AmA)的第一个300个值来计算两个显着的值;

2)获取Am值的索引,A这两个索引(向量是对称的,所以A会有两个索引)。

3)从另一载体(freq)获取的值与之前的三个索引(索引从Am相关联(fmf1f2),第一A和第二A分别)。

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值更受欢迎。

并提前致谢!

+0

发现我可以代替[〜,IM] = MAG(1:300,AM)通过 [〜,I1] = MAX(MAG(1:300)) 哪些工作正常。另外两个指标仍然存在问题。 –

+0

在你使用'mag'作为变量的第一段代码中,作为一个函数。这里没有什么真正的意义。 – thewaywewalk

+0

@ thewaywewalk,如果您正在尝试从* A *的两个值中获取索引,请忽略其中的内容。我似乎无法找到办法做到这一点,这是一个坏的尝试,我的坏。 –

回答

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

希望这有助于

+0

“使用查找时出错 第二个参数必须是一个正的标量整数”,当使用l2 = find(mag(1:300)== Am,'first'); * mag *是一个3908x1 double,即快速傅立叶变换的abs()。 另外,是否有可能取代'第一'的'第二',获得第二个索引? –

+0

l2 = find(mag(1:300)== Am,1,'first'); – siliconwafer