how velocize it? (it's possibile to vectorize it?)
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2023-7-30
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.
采纳的回答
Bruno Luong
2023-7-30
编辑:Bruno Luong
2023-7-31
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
[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
2 个评论
Bruno Luong
2023-7-30
编辑:Bruno Luong
2023-7-31
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;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!