find max by groups, subtract the max from values if a certain condition is satisfied

1 Ansicht (letzte 30 Tage)
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

Antworten (1)

Ameer Hamza
Ameer Hamza am 1 Mai 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);

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by