how to speed ...i need very fast code

1 visualización (últimos 30 días)
Luca Re
Luca Re el 30 de Ag. de 2023
Comentada: Luca Re el 31 de Ag. de 2023
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

Respuesta aceptada

Star Strider
Star Strider el 30 de Ag. de 2023
Editada: Star Strider el 30 de Ag. de 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')
LD = struct with fields:
matri: [6165×351 double]
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
Elapsed time is 3.543419 seconds.
COR
COR = 351×351
1.0000 0.5210 0.5090 0.5000 0.5000 0.5180 0.5010 0.5180 0.5150 0.5030 0.4950 0.4820 0.4870 0.4990 0.5020 0.4840 0.5020 0.5100 0.4980 0.4950 0.5140 0.5190 0.4920 0.5110 0.5070 0.5110 0.4900 0.5270 0.5200 0.5080 0.5210 1.0000 0.4900 0.4890 0.4810 0.5120 0.4730 0.5660 0.4860 0.4940 0.4740 0.5540 0.5320 0.5310 0.5290 0.5150 0.5120 0.5160 0.4970 0.5190 0.5190 0.4750 0.5250 0.5360 0.4580 0.5290 0.5120 0.5440 0.4720 0.5060 0.5090 0.4900 1.0000 0.5100 0.5160 0.5320 0.5320 0.5430 0.4900 0.5170 0.5620 0.4810 0.4930 0.4880 0.4990 0.4770 0.4640 0.4930 0.4940 0.4860 0.5120 0.5000 0.4950 0.5160 0.5060 0.5300 0.5040 0.4980 0.4820 0.4790 0.5000 0.4890 0.5100 1.0000 0.6350 0.5780 0.5720 0.4980 0.5090 0.5310 0.5430 0.5050 0.5050 0.4890 0.5110 0.5250 0.4900 0.4900 0.5000 0.4730 0.5010 0.5100 0.5090 0.5280 0.4930 0.4590 0.4940 0.4980 0.4440 0.5220 0.5000 0.4810 0.5160 0.6350 1.0000 0.5260 0.6010 0.6720 0.4570 0.5190 0.5480 0.5070 0.4980 0.4890 0.4760 0.5030 0.4590 0.5040 0.4970 0.4980 0.5180 0.4940 0.4940 0.4840 0.4790 0.4800 0.5470 0.4980 0.4310 0.5110 0.5180 0.5120 0.5320 0.5780 0.5260 1.0000 0.5210 0.4540 0.5660 0.5380 0.4030 0.5250 0.4960 0.4860 0.4870 0.4980 0.5090 0.4890 0.4800 0.5170 0.4890 0.5280 0.4910 0.4840 0.5210 0.4690 0.5380 0.4780 0.5560 0.5030 0.5010 0.4730 0.5320 0.5720 0.6010 0.5210 1.0000 0.5930 0.4810 0.5490 0.4970 0.5080 0.4930 0.4950 0.4940 0.5180 0.4810 0.4860 0.5090 0.4960 0.4810 0.4950 0.5130 0.5240 0.5090 0.5360 0.5090 0.5010 0.4850 0.5140 0.5180 0.5660 0.5430 0.4980 0.6720 0.4540 0.5930 1.0000 0.2900 0.4920 0.5990 0.5670 0.4830 0.4860 0.5000 0.5140 0.5080 0.4900 0.5130 0.5120 0.4940 0.4690 0.4670 0.5190 0.4570 0.4330 0.5070 0.5160 0.5000 0.5010 0.5150 0.4860 0.4900 0.5090 0.4570 0.5660 0.4810 0.2900 1.0000 0.5280 0.4470 0.4890 0.5200 0.5020 0.5140 0.5150 0.5260 0.5140 0.5020 0.5260 0.4950 0.5030 0.5190 0.5050 0.4850 0.5150 0.4770 0.4970 0.5380 0.5150 0.5030 0.4940 0.5170 0.5310 0.5190 0.5380 0.5490 0.4920 0.5280 1.0000 0.4810 0.5040 0.5000 0.5120 0.5150 0.4830 0.4950 0.4860 0.4960 0.4980 0.4890 0.5130 0.5040 0.5030 0.5290 0.4920 0.5180 0.5090 0.5220 0.5030
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
Elapsed time is 1.354815 seconds.
COR
COR = 351×351
1.0000 0.5214 0.5091 0.5004 0.5000 0.5175 0.5008 0.5180 0.5149 0.5029 0.4950 0.4825 0.4866 0.4991 0.5015 0.4843 0.5015 0.5105 0.4979 0.4950 0.5139 0.5189 0.4918 0.5114 0.5071 0.5105 0.4899 0.5267 0.5199 0.5084 0.5214 1.0000 0.4899 0.4886 0.4810 0.5116 0.4734 0.5665 0.4858 0.4944 0.4737 0.5544 0.5316 0.5314 0.5294 0.5146 0.5117 0.5158 0.4974 0.5185 0.5191 0.4747 0.5247 0.5361 0.4582 0.5287 0.5117 0.5441 0.4718 0.5063 0.5091 0.4899 1.0000 0.5104 0.5159 0.5322 0.5320 0.5432 0.4901 0.5170 0.5622 0.4811 0.4930 0.4883 0.4993 0.4773 0.4639 0.4934 0.4944 0.4857 0.5116 0.5000 0.4949 0.5161 0.5056 0.5304 0.5044 0.4980 0.4819 0.4786 0.5004 0.4886 0.5104 1.0000 0.6349 0.5780 0.5720 0.4984 0.5089 0.5313 0.5430 0.5051 0.5052 0.4885 0.5109 0.5252 0.4895 0.4898 0.4996 0.4734 0.5015 0.5104 0.5094 0.5282 0.4931 0.4593 0.4944 0.4979 0.4444 0.5220 0.5000 0.4810 0.5159 0.6349 1.0000 0.5259 0.6009 0.6725 0.4570 0.5192 0.5476 0.5069 0.4976 0.4890 0.4761 0.5030 0.4594 0.5040 0.4975 0.4978 0.5176 0.4943 0.4941 0.4838 0.4789 0.4800 0.5473 0.4985 0.4305 0.5109 0.5175 0.5116 0.5322 0.5780 0.5259 1.0000 0.5210 0.4540 0.5655 0.5380 0.4029 0.5251 0.4961 0.4855 0.4871 0.4982 0.5087 0.4888 0.4797 0.5167 0.4890 0.5276 0.4913 0.4835 0.5209 0.4685 0.5381 0.4777 0.5563 0.5030 0.5008 0.4734 0.5320 0.5720 0.6009 0.5210 1.0000 0.5931 0.4810 0.5494 0.4968 0.5082 0.4934 0.4948 0.4937 0.5185 0.4807 0.4862 0.5094 0.4961 0.4811 0.4954 0.5132 0.5243 0.5086 0.5358 0.5086 0.5015 0.4847 0.5144 0.5180 0.5665 0.5432 0.4984 0.6725 0.4540 0.5931 1.0000 0.2897 0.4920 0.5989 0.5667 0.4829 0.4862 0.5000 0.5144 0.5081 0.4904 0.5133 0.5117 0.4943 0.4694 0.4669 0.5195 0.4573 0.4330 0.5066 0.5159 0.5000 0.5010 0.5149 0.4858 0.4901 0.5089 0.4570 0.5655 0.4810 0.2897 1.0000 0.5284 0.4468 0.4890 0.5205 0.5022 0.5140 0.5147 0.5258 0.5141 0.5022 0.5261 0.4949 0.5029 0.5187 0.5053 0.4851 0.5148 0.4771 0.4972 0.5375 0.5152 0.5029 0.4944 0.5170 0.5313 0.5192 0.5380 0.5494 0.4920 0.5284 1.0000 0.4811 0.5037 0.5000 0.5121 0.5149 0.4826 0.4946 0.4856 0.4957 0.4976 0.4886 0.5129 0.5043 0.5033 0.5292 0.4918 0.5183 0.5092 0.5216 0.5033
EDIT — Slight tweak to create all the conditionals together.
.
  2 comentarios
Luca Re
Luca Re el 30 de Ag. de 2023
I saw that the vectorization can not be done ..ne get times of 1/10 from my ..patience I accept this last
Star Strider
Star Strider el 30 de Ag. de 2023
Thank you!

Iniciar sesión para comentar.

Más respuestas (2)

Bruno Luong
Bruno Luong el 30 de Ag. de 2023
Editada: Bruno Luong el 30 de Ag. de 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
Elapsed time is 2.403425 seconds.
tic
s = sign(matrix);
b = double(matrix~=0);
cor = round((1 + (s'*s)./(b'*b))*500)/1000;
toc
Elapsed time is 0.025416 seconds.
% Does it match?
isequaln(cor,COR)
ans = logical
1
  4 comentarios
Bruno Luong
Bruno Luong el 31 de Ag. de 2023
Editada: Bruno Luong el 31 de Ag. de 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
ans = logical
1
any(isnan(cornan),'all') % nan disappears
ans = logical
0
% Does it match? 0 is perfectly match
max(abs(cor-cornan),[],'all')
ans = 0
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;
Luca Re
Luca Re el 31 de Ag. de 2023
Thank you

Iniciar sesión para comentar.


David Hill
David Hill el 30 de Ag. de 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 comentario
Luca Re
Luca Re el 30 de Ag. de 2023
Editada: Luca Re el 30 de Ag. de 2023
hi,
Elapsed time is 4.489091 seconds. My Code
Elapsed time is 4.406116 seconds. Your Code
time is similar

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by