how can i vectorize it?

7 vues (au cours des 30 derniers jours)
pipin
pipin le 7 Août 2023
Commenté : pipin le 8 Août 2023
HELLO, IT TOOK A LITTLE BEFORE I CODED IT CORRECTLY
I hope I haven't made any mistakes
%%how velocize matrix of array
%I BUILD DATA IN THIS EXAMPLE
row=10;
col=8;
profit = rand(row,col)
profit = 10×8
0.3436 0.1311 0.9776 0.6418 0.2922 0.8690 0.4831 0.5124 0.9126 0.7900 0.3755 0.3017 0.7737 0.8073 0.8464 0.5552 0.8071 0.9526 0.2701 0.4222 0.4250 0.9831 0.4610 0.1624 0.1542 0.1770 0.9003 0.6316 0.6100 0.8506 0.0065 0.7861 0.5361 0.1805 0.1426 0.6149 0.7982 0.9206 0.3312 0.0705 0.3135 0.0977 0.6855 0.9359 0.2368 0.2731 0.9951 0.7932 0.1589 0.8174 0.4833 0.2497 0.0689 0.0655 0.7721 0.8835 0.9792 0.7773 0.1901 0.0859 0.6659 0.8166 0.6375 0.5151 0.6811 0.0662 0.0089 0.6860 0.7540 0.0032 0.8470 0.7785 0.8207 0.2034 0.2639 0.4568 0.2538 0.2146 0.0026 0.6747
c3 = randi(size(profit,2),row,col);
maxcat = 3;
fr = ones(size(profit));
group=row-1; %group must to be <row)
c3(rand(group,col)<0.4)=0 % i use this code to turn off some element in c3
c3 = 10×8
2 3 0 0 6 0 6 8 0 8 7 8 4 6 0 4 7 6 3 8 0 5 0 1 0 7 6 2 6 0 1 3 5 1 0 0 0 4 0 4 5 4 0 3 6 0 2 2 0 5 2 0 0 7 0 8 0 0 4 6 0 5 6 8 3 0 0 0 8 0 3 2 1 7 0 2 0 0 0 6
%%*****************************
%*****************************THIS IS CODE TO VELOCIZE
for r=1:row
for x=1:group %(rank group)
z=c3(x,c3(x,:)>0); %find element >0
A=profit(r,z);
[~,idxx]=sort(A,'descend');
idxxx=idxx(maxcat+1:end);
if ~isempty(idxxx)
fr(r,z(idxxx))=0;
end
end
end
% EDIT: View results
fr
fr = 10×8
0 0 1 1 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 1 0 0 1 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 1 0 1 1 1 0 1 0 0 0 0 1
  1 commentaire
pipin
pipin le 7 Août 2023
Modifié(e) : pipin le 7 Août 2023

Connectez-vous pour commenter.

Réponse acceptée

Bruno Luong
Bruno Luong le 8 Août 2023
Modifié(e) : Bruno Luong le 8 Août 2023
If you prefer ugly and unreadable code
%I BUILD DATA IN THIS EXAMPLE
row=10;
col=8;
profit = rand(row,col);
c3 = randi(size(profit,2),row,col);
maxcat = 3;
fr = ones(size(profit));
group=row-1; %group must to be <row)
c3(rand(group,col)<0.4)=0; % i use this code to turn off some element in c3
%%*****************************
%*****************************THIS IS CODE TO VELOCIZE
for r=1:row
for x=1:group %(rank group)
z=c3(x,c3(x,:)>0); %find element >0
A=profit(r,z);
[~,idxx]=sort(A,'descend');
idxxx=idxx(maxcat+1:end);
if ~isempty(idxxx)
fr(r,z(idxxx))=0;
end
end
end
% EDIT: View results
fr
fr = 10×8
1 0 1 1 0 0 0 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 1 0 0
fr = ones(size(profit));
% vectorize method
[~,j,c] = find(c3(1:group,:)');
c = accumarray(j, c, [], @(i){i});
k = max(cellfun(@height, c)-maxcat, 0);
[~, j] = arrayfun(@(c,k)mink(profit(:,c{1}), k, 2), c, k, 'unif', 0);
cj = cellfun(@(c,j) c(j), c, j, 'unif', 0);
fr((1:row)' + row*([cj{:}]-1)) = 0;
fr
fr = 10×8
1 0 1 1 0 0 0 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 1 0 0
  2 commentaires
Mike Croucher
Mike Croucher le 8 Août 2023
It may be ugly and unreadable but its many times faster! Nice work
I took your code and increased the problem size to give it something more meaty to process: setting
row=1000;
col=8;
give the following times on my machine
origTime =
1.7324
vectorTime =
0.0613
Vectorised method is 28.254690 times faster
Full code for anyone who wants to reproduce this:
%%how velocize matrix of array
%I BUILD DATA IN THIS EXAMPLE
rng("default") %For reproducibility
row=1000;
col=8;
profit = rand(row,col);
c3 = randi(size(profit,2),row,col);
maxcat = 3;
group=row-1; %group must to be <row)
c3(rand(group,col)<0.4)=0; % i use this code to turn off some element in c3
%*****************************THIS IS CODE TO VELOCIZE
fr = ones(size(profit));
tic
for r=1:row
for x=1:group %(rank group)
z=c3(x,c3(x,:)>0); %find element >0
A=profit(r,z);
[~,idxx]=sort(A,'descend');
idxxx=idxx(maxcat+1:end);
if ~isempty(idxxx)
fr(r,z(idxxx))=0;
end
end
end
origTime = toc
origTime = 4.2989
fr = ones(size(profit));
% vectorize method
tic
[~,j,c] = find(c3(1:group,:)');
c = accumarray(j, c, [], @(i){i});
k = max(cellfun(@height, c)-maxcat, 0);
[~, j] = arrayfun(@(c,k)mink(profit(:,c{1}), k, 2), c, k, 'unif', 0);
cj = cellfun(@(c,j) c(j), c, j, 'unif', 0);
fr((1:row)' + row*([cj{:}]-1)) = 0;
vectorTime = toc
vectorTime = 0.1545
fprintf("Vectorised method is %f times faster\n",origTime/vectorTime);
Vectorised method is 27.815283 times faster
pipin
pipin le 8 Août 2023
good work ..thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by