【问题标题】:Matlab: indexes and associated valuesMatlab:索引和相关值
【发布时间】:2015-04-27 10:10:03
【问题描述】:

首先,我的编程能力不是很好,所以请多多包涵。

我正在尝试在 Matlab,R2015a 中编写以下脚本:

1) 给定某个向量 (mag),从该向量的前 300 个值(AmA)中计算出两个显着值;

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

3) 从另一个向量(@ 987654333@).

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 值。欢迎提供任何提示和建议。

提前谢谢你!

【问题讨论】:

  • 发现我可以用 [~,I1] = max(mag(1:300)) 替换 [~,Im] = mag(1:300,Am) 效果很好。其他两个索引的问题仍然存在。
  • 在您的第一段代码中,您使用mag 作为变量,进一步作为函数。这里没有任何意义。
  • @thewaywewalk,如果您想从 A 的两个值中获取索引,请忽略其中的内容。我似乎找不到办法,这是一次糟糕的尝试,我的错。

标签: matlab indexing


【解决方案1】:

这里有一些建议:

%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 的示例?

希望对你有帮助

【讨论】:

  • "错误使用 find 第二个参数必须是一个正标量整数。",当使用 l2=find(mag(1:300)==Am, 'first'); mag 是一个 3908x1 的双精度数,即快速傅里叶变换的 abs()。另外,是否可以将'first'替换为'second',得到第二个索引?
  • l2=find(mag(1:300)==Am, 1, 'first');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 2014-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-07
  • 1970-01-01
相关资源
最近更新 更多