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

2 Ansichten (letzte 30 Tage)
sia ben
sia ben am 14 Jan. 2020
Kommentiert: sia ben am 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!

Akzeptierte Antwort

Akira Agata
Akira Agata am 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
--------------------------------

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by