how to speed ...i need very fast code
Afficher commentaires plus anciens
q=6062; %%I want to call this code and pass it a different 'q' each time
matrix=matri(1:q,:);
[~,c]=size(matrix);
COR=zeros(c,c);
tic
gg=1:c;
a=matrix(:,gg);
for yy=1:c
b=matrix(:,yy);
cc=sum((a>0 & b>0)|(a<0 & b<0),1);
cc1=sum(a~=0 & b~=0,1);
COR(yy,gg)=round(cc./cc1*1000)/1000; %round(100 * cc./cc1)/100; %%array multidimension
end
toc
Réponse acceptée
Plus de réponses (2)
Bruno Luong
le 30 Août 2023
Modifié(e) : Bruno Luong
le 30 Août 2023
You want REALLY FAST code?
Watch this, almost 100 time faster
load('matlab_matri.mat')
q=6062; %%I want to call this code and pass it a different 'q' each time
matrix=matri(1:q,:);
[~,c]=size(matrix);
tic
COR=zeros(c,c); % you have to count this as well
gg=1:c;
a=matrix(:,gg);
for yy=1:c
b=matrix(:,yy);
cc=sum((a>0 & b>0)|(a<0 & b<0),1);
cc1=sum(a~=0 & b~=0,1);
COR(yy,gg)=round(cc./cc1*1000)/1000; %round(100 * cc./cc1)/100; %%array multidimension
end
toc
tic
s = sign(matrix);
b = double(matrix~=0);
cor = round((1 + (s'*s)./(b'*b))*500)/1000;
toc
% Does it match?
isequaln(cor,COR)
4 commentaires
shamal
le 31 Août 2023
Bruno Luong
le 31 Août 2023
Modifié(e) : Bruno Luong
le 31 Août 2023
No it is not correct.
Correct one is this
% cor = round((1 + (s'*s)./((b'*b)+eps(0.5)))*500)/1000;
This will return identical numerical result, excepted when working on colum dot-product that contain all 0s.
The reason is the minimum strictly positive of (b'*b) is 1, adding eps(0.5) to it does not change the value, unless it is 0 where it protect the denominator to vanishe, as showed here
load('matlab_matri.mat')
q=6062; %%I want to call this code and pass it a different 'q' each time
matrix=matri(1:q,:);
[~,c]=size(matrix);
s = sign(matrix);
b = double(matrix~=0);
cor = round((1 + (s'*s)./(b'*b))*500)/1000;
s = sign(matrix);
b = double(matrix~=0);
cornan = round((1 + (s'*s)./((b'*b)+eps(0.5)))*500)/1000;
any(isnan(cor),'all') % nan is present
any(isnan(cornan),'all') % nan disappears
% Does it match? 0 is perfectly match
max(abs(cor-cornan),[],'all')
Note that in my case cornan contain 1 for correlation of two colums of matrix that do not share 1s, and not 0 as with your code. It is somewhat an arbitrary choice, since the correlation is undefined. Without protection MATLAB NaN result is actually more "correct" IMO since it reflects this fact of arbitrary choice.
If it bother you, just do not protect, then simply add this at the end:
cor(isnan(cor)) = 0;
Alternatively you can do this to return 0 for degenerated case
s = sign(matrix);
b = double(matrix~=0);
btb = b'*b;
cornan = round(((btb + (s'*s))./(btb+eps(0.5)))*500)/1000;
shamal
le 31 Août 2023
David Hill
le 30 Août 2023
q=6062;
matrix=matri(1:q,:);
c=size(matrix,2);
COR=zeros(c);
a=matrix;
for yy=1:c
b=matrix(:,yy);
COR(yy,:)=round(sum((a>0 & b>0)|(a<0 & b<0),1)./sum(a~=0 & b~=0,1),3);
end
1 commentaire
Catégories
En savoir plus sur App Building dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!