find max of every 4 rows in a matrix and save in separate array

1 visualizzazione (ultimi 30 giorni)
Im trying to find max of 5th column of every 4 rows in a matrix say A. I need to push the max rows into a separate matrix B and remove these rows from A. I have attached sample data herewith. The data is for 6 customers with 4 tags(12,22,32,42) for each customer. I need to take the max of percentage between the four tags for each customer. So far I have tried with a for loop as :
custs = unique(data(:,3));
highertrp = zeros(size(custs,1),1);
for i = 1 : size(custs,1)
idx = find(data(:,3)== custs(i,1));
[~,id] = max(data(idx,5),[],1);
highertrp(i,1:size(data,2)) = data(idx(id),:);
data(idx(id),:) = [];
end
But this is too slow as I can have about 1 million records to check in my project. Any help will be great.

Risposta accettata

Andrei Bobrov
Andrei Bobrov il 18 Apr 2016
Modificato: Andrei Bobrov il 18 Apr 2016
data = xlsread('sample_data.xlsx',1);
[~,~,c] = unique(data(:,3));
idx = accumarray(c,(1:size(data,1))',[], @(x) x( max(data(x,5))==data(x,5) ,:) );
highertrp = data(idx,:);
data(idx,:) = [];
in case if find max of every 4 rows
data = xlsread('sample_data.xlsx',1);
[~,ii] = max(reshape(data(:,5),4,[]));
idx = ii + (0:numel(ii)-1)*4;
highertrp = data(idx,:);
data(idx,:) = [];
  3 Commenti
Hariprasad
Hariprasad il 18 Apr 2016
Modificato: Hariprasad il 18 Apr 2016
Oh ok the indices of each Id. Thank you @Andrei Bobrov

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by