How to match pairs of elements to known corresponding values?
1 回表示 (過去 30 日間)
古いコメントを表示
I have an output of a matrix with two frequencies that correspond to a specific number based on its frequencies. Each column in the matrix corresponds to 2 frequencies that outputs a number. Here is the matrix I have defined:
spec_freq =
1.0e+03 *
0.8516 1.3353 0.6957 0.8516 1.3353 1.2074 0.8516 0.8516 1.3353 1.2074
1.3353 0.9395 1.2074 1.2074 0.7716 0.7716 1.3353 1.3353 0.6957 0.7716
I know that the numbers correspond to the following frequencies (it doesn't matter which order)
1 = 697 1209
2 = 697 1336
3 = 697 1477
4 = 770 1209
5 = 770 1336
6 = 770 1477
7 = 852 1209
8 = 852 1336
9 = 852 1477
0 = 941 1336
I need to make a loop that scans through the spec_freq columns and match the frequencies to its corresponding number. The numbers in the spec_freq are sometimes not the exact frequency for a number, but always very close in range. I know the spec_freq matrix I listed above does not contain all the numbers, but I need this loop to work with any spec_freq matrix, incase its frequencies contain a different number.
0 件のコメント
回答 (1 件)
Birdman
2018 年 4 月 5 日
This problem does not need a for loop to be solved. Instead, you can use ismembertol for this purpose.
spec_freq =1.0e+03 *[0.8516 1.3353 0.6957 0.8516 1.3353 1.2074 0.8516 0.8516 1.3353 1.2074;1.3353 0.9395 1.2074 1.2074 0.7716 0.7716 1.3353 1.3353 0.6957 0.7716];
A=[1 697 1209
2 697 1336
3 697 1477
4 770 1209
5 770 1336
6 770 1477
7 852 1209
8 852 1336
9 852 1477
10 941 1336].';
tol=5e-3;
numbers=A(1,all(ismembertol(A(2:3,:),spec_freq,tol),1))
Of course I specified tol as 5e-3 but it may change. Change it and observe the result you want. Hope this helps.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!