how can i vectorize it?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
pipin
el 7 de Ag. de 2023
Comentada: pipin
el 8 de Ag. de 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)
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 comentario
Respuesta aceptada
Bruno Luong
el 8 de Ag. de 2023
Editada: Bruno Luong
el 8 de Ag. de 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 comentarios
Mike Croucher
el 8 de Ag. de 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);
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!