find max by groups, subtract the max from values if a certain condition is satisfied
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I have a matrix as below (add column name for the convenience here). With the matrix, if max of value for each group is larger than 700, I want to have desired_column = (value - 700) (group 1 and group 3 have larger than 700 for the max(value), since max(value) are 1300 and 900 respectively). However, I do not want to subtract 700 when outside is 1.
group outside value desired_column
1 0 1300
1 0 800
1 1 0
2 0 150
2 0 600
2 1 0
3 0 800
3 0 900
3 1 0
Here is what I want to have
1 0 1300 600
1 0 800 100
1 1 0 0
2 0 150 150
2 0 600 600
2 1 0 0
3 0 800 100
3 0 900 200
3 1 0 0
0 Commenti
Risposte (1)
Ameer Hamza
il 1 Mag 2018
This will process the matrix as specified in question
partition = splitapply(@(x) {x}, A, A(:, 1));
for i = 1:length(partition)
partition{i}(index, 4) = partition{i}(index, 3);
index = max(partition{i}(:,3)) > 700 & partition{i}(:,2)~=1;
partition{i}(index, 4) = partition{i}(index, 3) - 700;
end
cell2mat(partition);
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!