how can i vectorize it?
1 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Bruno Luong
2023-8-8
编辑:Bruno Luong
2023-8-8
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 个评论
Mike Croucher
2023-8-8
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!