how can i vectorize it?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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)
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
1 Kommentar
Akzeptierte Antwort
Bruno Luong
am 8 Aug. 2023
Bearbeitet: Bruno Luong
am 8 Aug. 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 = 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
2 Kommentare
Mike Croucher
am 8 Aug. 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
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
fprintf("Vectorised method is %f times faster\n",origTime/vectorTime);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!