How can I extract the harmonic related numbers from a matrix?

2 ビュー (過去 30 日間)
sia ben
sia ben 2020 年 1 月 14 日
コメント済み: sia ben 2020 年 1 月 15 日
Hallo,
I have a array of numbers for example: (90 100 110 200 220 250 300 330 340 400 420 500)
I wand to make a function who find the harmonic related numbers with its index.
answer: (100 200 300 400 500) and ther index: (2 4 7 10 12). Theoreticaly it should find the second one aswell: (110 220 330) and index (3 5 8)
Thanks!

採用された回答

Akira Agata
Akira Agata 2020 年 1 月 15 日
How about the following?
x = [90 100 110 200 220 250 300 330 340 400 420 500];
tfUsed = false(size(x));
R = x./x';
idx = R == round(R);
pt = 1;
disp('--------------------------------')
while ~all(tfUsed)
disp(['Answer: ',num2str(x(idx(pt,:)))])
disp(['Index : ',num2str(find(idx(pt,:)))])
disp('--------------------------------')
tfUsed(idx(pt,:)) = true;
pt = find(~tfUsed,1);
end
The result is as follows:
--------------------------------
Answer: 90
Index : 1
--------------------------------
Answer: 100 200 300 400 500
Index : 2 4 7 10 12
--------------------------------
Answer: 110 220 330
Index : 3 5 8
--------------------------------
Answer: 250 500
Index : 6 12
--------------------------------
Answer: 340
Index : 9
--------------------------------
Answer: 420
Index : 11
--------------------------------

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by