how to speed ...i need very fast code
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Star Strider
on 30 Aug 2023
Edited: Star Strider
on 30 Aug 2023
Putting the ‘a’ conditional tests outside the loop (and still within the tic-toc block) sppeds it up a bit —
LD = load('matlab_matri.mat')
matri = LD.matri;
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
COR
tic
agt0 = matrix>0;
alt0 = matrix<0;
ane0 = matrix~=0;
for yy = 1:c
cc=sum((agt0 & agt0(:,yy))|(alt0 & alt0(:,yy)),1);
cc1=sum(ane0 & ane0(:,yy),1);
COR(:,yy) = cc./cc1;
end
COR = round(COR,4);
toc
COR
EDIT — Slight tweak to create all the conditionals together.
.
More Answers (2)
Bruno Luong
on 30 Aug 2023
Edited: Bruno Luong
on 30 Aug 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 Comments
Bruno Luong
on 31 Aug 2023
Edited: Bruno Luong
on 31 Aug 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;
David Hill
on 30 Aug 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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!