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!