Finding rows of minimum valueS by group
2 views (last 30 days)
Show older comments
I want to select the rows with the lowest values (more than one) by group
In the following for each date3( col:2), I want to select the rowS with the lowest TimeToMaturity(6)

0 Comments
Answers (2)
KSSV
on 14 Dec 2022
Let date2 and TimeToMaturity be your two column arrays.
[c,ia,ib] = unique(date2) ;
N = length(c) ;
iwant = zeros(N,1) ;
for i = 1:N
iwant(i) = min(TimeToMaturity(ib==i)) ;
end
[c iwant]
4 Comments
Voss
on 14 Dec 2022
Edited: Voss
on 14 Dec 2022
Some data like your date3 and TimeToMaturity columns (just using 1, 2, 3, 4 for date here):
date = reshape(repmat(1:4,5,1),[],1);
time_to_maturity = randi(100,20,1);
disp([date time_to_maturity]);
Now find the minimum time_to_maturity for each date and the row where it occurs:
[ud,~,jj] = unique(date);
N = numel(ud);
val = zeros(N,1);
row = zeros(N,1);
for ii = 1:N
group_idx = find(jj == ii);
[val(ii),temp_idx] = min(time_to_maturity(group_idx));
row(ii) = group_idx(temp_idx);
end
fprintf('Date %d: minimum TimeToMaturity is %02d, which occurs in row %02d\n',[ud val row].');
0 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!