find max by groups, subtract the max from values if a certain condition is satisfied
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Ameer Hamza
2018 年 5 月 1 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!