How can I extract the harmonic related numbers from a matrix?
2 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Akira Agata
on 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
--------------------------------
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!