Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
sia ben
sia ben le 14 Jan 2020
Commenté : sia ben le 15 Jan 2020
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!

Réponse acceptée

Akira Agata
Akira Agata le 15 Jan 2020
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
--------------------------------

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by