필터 지우기
필터 지우기

Extracting row of a matrix based on maximum value of a column element

조회 수: 4 (최근 30일)
Hello all:
I have a matrix as below. I want to build a new matrix based on maximum value at row 4 element. Here column 5 is the index matrix, column 1 is the year, column 2 is month and column 3 is date. I want to extract the row with maximum value in column 4. For example, the value with 2 in column 5 occurred twice. The respective values at column 4 are 50 and 100. I want to extract the row with value 100.
A=
1978 7 1 45 1
1978 7 11 50 2
1978 7 14 100 2
1978 7 23 120 3
1978 8 17 150 4
1978 8 23 130 5
1978 8 24 40 5
1978 8 25 200 5
Desired output B =
1978 7 1 45 1
1978 7 14 100 2
1978 7 23 120 3
1978 8 17 150 4
1978 8 25 200 5
  댓글 수: 1
Poulomi Ganguli
Poulomi Ganguli 2018년 8월 24일
Hello, I solved the problem using ismember
unq_col = unique(Data_All(:,5));
for jdx = 1:size(unq_col,1)
matched = ismember(Data_All(:,5),unq_col(jdx,1),'rows');
des_flow = Data_All(matched,:);
[Maxflow,ID] = max(des_flow(:,4));
OUT(jdx,:) = des_flow(ID,:);
end

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 8월 24일
>> mat = [...
1978 7 1 45 1
1978 7 11 50 2
1978 7 14 100 2
1978 7 23 120 3
1978 8 17 150 4
1978 8 23 130 5
1978 8 24 40 5
1978 8 25 200 5]
>> tmp = sortrows(mat,[5,4]);
>> idx = [diff(tmp(:,5))>0;true];
>> out = tmp(idx,:)
out =
1978 7 1 45 1
1978 7 14 100 2
1978 7 23 120 3
1978 8 17 150 4
1978 8 25 200 5

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by