how velocize it? (it's possibile to vectorize it?)

1 view (last 30 days)
Ntradess =[2 99 1 8 15 67 74 51 58 40
3 80 7 14 16 73 55 57 64 41
3 80 7 20 22 54 56 63 70 47
3 81 7 21 3 60 62 69 71 28
3 81 9 2 9 61 68 75 52 34
4 82 9 83 90 42 49 26 33 65
4 82 9 89 91 48 30 32 39 66
5 85 10 95 97 29 31 38 45 72
6 85 11 96 78 35 37 44 46 53
7 85 12 77 84 36 43 50 27 58
8 86 13 80 84 38 45 51 30 58
8 86 13 82 84 38 45 51 31 58
8 86 13 83 84 38 45 51 31 58
9 87 13 84 84 38 45 51 31 58];
RP_bin=[0 0 1 0 0 1 0 0 1 0 0 1 0 0]';
period=2;
minTrades=2;
g=find(RP_bin>0);
Ntradess_=Ntradess(g,:);
[r,c]=size(Ntradess_);
MinNtrad=zeros(r,c);
for x=1:c %loop colonne
for i=period+1:r
a=Ntradess_(i,x); %n:trade del idx attuale
if a>minTrades
a=find(flip(Ntradess_(1:i-1,x))<=(a-minTrades),1);
if ~isempty(a)
MinNtrad(i,x)= i-a; %MEMORIZZO la posizione della schiera per avere in mintrades richiesti
end
end
end
end
  4 Comments
Walter Roberson
Walter Roberson on 30 Jul 2023
You could save some coding effort if you were to use
a = find(Ntradess_(1:i-1,x))<=(a-minTrades), 1, 'last');
if ~isempty(a)
MinNtrad(i,x) = a;
end
The change in logic might make it easier to figure out how to vectorize.

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 30 Jul 2023
Edited: Bruno Luong on 31 Jul 2023
I don't know the memory requirement would go up if I vectorize the outer loop in a non-toy case. So I leave it for now.
Ntradess =[2 99 1 8 15 67 74 51 58 40
3 80 7 14 16 73 55 57 64 41
3 80 7 20 22 54 56 63 70 47
3 81 7 21 3 60 62 69 71 28
3 81 9 2 9 61 68 75 52 34
4 82 9 83 90 42 49 26 33 65
4 82 9 89 91 48 30 32 39 66
5 85 10 95 97 29 31 38 45 72
6 85 11 96 78 35 37 44 46 53
7 85 12 77 84 36 43 50 27 58
8 86 13 80 84 38 45 51 30 58
8 86 13 82 84 38 45 51 31 58
8 86 13 83 84 38 45 51 31 58
9 87 13 84 84 38 45 51 31 58];
RP_bin=[0 0 1 0 0 1 0 0 1 0 0 1 0 0]';
period=2;
minTrades=2;
g=find(RP_bin>0);
Ntradess_=Ntradess(g,:);
[r,c]=size(Ntradess_);
MinNtrad=zeros(r,c);
for x=1:c %loop colonne
for i=period+1:r
a=Ntradess_(i,x); %n:trade del idx attuale
if a>minTrades
a=find(flip(Ntradess_(1:i-1,x))<=(a-minTrades),1);
if ~isempty(a)
MinNtrad(i,x)= i-a; %MEMORIZZO la posizione della schiera per avere in mintrades richiesti
end
end
end
end
MinNtrad
MinNtrad = 4×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 2 1 0 0 2 2 1 3 2 3 1 3 3 3 3 0 3
[r,c]=size(Ntradess_);
MinNtrad = zeros(r,c);
Mask = flip(triu(true(r),1),1);
for x = 1:c
nx = Ntradess_(:,x);
[v,j] = max(Mask .* (nx'-flip(nx,1) >= minTrades), [], 1);
j = r+1-j;
j(v(:)==0 | nx(:)<=minTrades) = 0;
MinNtrad(:,x) = j;
end
MinNtrad(1:period,:) = 0;
MinNtrad
MinNtrad = 4×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 2 1 0 0 2 2 1 3 2 3 1 3 3 3 3 0 3
  2 Comments
Bruno Luong
Bruno Luong on 30 Jul 2023
Edited: Bruno Luong on 31 Jul 2023
Full vectorize
warning: potential runout of memory
[r,c] = size(Ntradess_);
Mask = (1:r)' > (r:-1:1); % flip(triu(true(r),1),1);
D = reshape(Ntradess_, [1 r c]) - reshape(flip(Ntradess_, 1), [r 1 c]);
B = Mask .* (D >= minTrades);
[V,J] = max(B, [], 1);
MinNtrad = r+1-reshape(J, [r c]);
V = reshape(V, [r c]);
MinNtrad(V==0 | Ntradess_<=minTrades) = 0;
MinNtrad(1:period,:) = 0;
pipin
pipin on 31 Jul 2023
Edited: pipin on 31 Jul 2023
congrats for the vectorization....but i don't use it it requires a lot of free memory

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!