Comparing two matrices, as fast as possible
17 views (last 30 days)
Show older comments
Hello Everyone
I am currently writing a music comparing AI code on MATLAB. It is my graduation project.
Firstly, I printed filtered with grayscale 1,0 spectrogram graph of our saved music with the function i write and i recorded 5 second sound. I also processed same way to the sound I recorded from external mic.
SPECTROGRAM OF MUSIC:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/547598/image.jpeg)
5 SECOND RECORDED FILE:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/547603/image.jpeg)
They are both 129xSamples
How can i compare and search from the exact music matrix as fast as possible? I wrote the little function but i cant use with this way. It took apx 56 second and its too long for one song.
Here my little code:
function compare_algorithm(Music1,Sample1)
%Takes data from file
load("datas_audio/"+Music1)
load("datas_audio/"+Sample1)
%Deletes .mat part
Music1 = extractBefore(Music1,".");
Sample1 = extractBefore(Sample1,".");
M1=eval(Music1);
S1=eval(Sample1);
[a,b] = size(M1);
[c,d] = size(S1);
sira = 1;
list1=[];
%Comparing row by row until the finish to findout how long will it take
for SS = 1:b-d+1
count = 0;
for jj = sira:d+(sira-1)
for ii = 1:129
if M1(ii,jj)==S1(ii,jj-sira+1)
count=count+1;
end
end
end
sira=sira+1;
CC=count/(c*d);
list1(end+1) = CC;
end
MAX=max(list1)
end
What is the best way to compare two matrices?
I am planning to compare from 100 to 200 music sample as fast as possible. I am open to any recommendations.
Thanks a lot
Altemur :D
7 Comments
Walter Roberson
on 12 Mar 2021
In that case I recommend that you do not worry as much about using a technique that is "as fast as possible". If you get into "as fast as possible" then your entire approach might have to be rewritten if you swap your SIMM chips into different banks, since that could affect the timing of caches. An approach that takes you 4 years to build and saves 3 nanoseconds per year is a better candidate for "as fast as possible" compared to an approach that only takes you 2 years to build.
Answers (1)
Shubham Rawat
on 16 Mar 2021
Hi Altemur,
I have written a code and I had done intersction of both the matrices column wise without using the for loop.
%loading matrices
load('counting_stars.mat');
load('girenfile.mat');
%transposing the matrices so that number of columns should be same.
a = counting_stars';
b = girenfile';
%intersecting both matrices
[c, ia, ib] = intersect(a, b, 'rows');
c will give the common columns, ia and ib will give all the indices which are common in both a and b.
Hope this Helps!
See Also
Categories
Find more on Measurements and Spatial Audio in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!