f



how to find the maximal value wrt. the value in another column

Hi all, for instance, I have a matrix A:
[1   4; 
 2   6; 
 1   1; 
 2   9; 
 1   3;
 2   7; 
 1   2; 
 2   8; 
 1   10; 
 2   5]

What I want is to find the maximal value in the second column wrt. the index in the first column:
[1  10;
 2   9]
0
Yufeng
12/20/2016 9:08:03 AM
comp.soft-sys.matlab 211266 articles. 23 followers. lunamoonmoon (257) is leader. Post Follow

3 Replies
423 Views

Similar Articles

[PageSpeed] 59

On 12/20/2016 3:08 AM, Yufeng Xiong wrote:
> Hi all, for instance, I have a matrix A:
> [1   4;
>  2   6;
>  1   1;
>  2   9;
>  1   3;
>  2   7;
>  1   2;
>  2   8;
>  1   10;
>  2   5]
>
> What I want is to find the maximal value in the second column wrt. the index in the first column:
> [1  10;
>  2   9]
>

one way might be

B=unique(A,'rows');
[B(find(diff(B(:,1))==1),:);B(end,:)]

ans =

      1    10
      2     9

This also should work

B=sortrows(A);
[B(find(diff(B(:,1))==1),:);B(end,:)]

ps. did not debug it :)

--Nasser








0
Nasser
12/20/2016 9:28:20 AM
>> A=[1 4;
 2 6;
 1 1;
 2 9;
 1 3;
 2 7;
 1 2;
 2 8;
 1 10;
 2 5]

A =

     1     4
     2     6
     1     1
     2     9
     1     3
     2     7
     1     2
     2     8
     1    10
     2     5

>> [u,~,J] = unique(A(:,1));
>> [u, accumarray(J,A(:,2),[],@max)]

ans =

     1    10
     2     9

% Bruno
0
Bruno
12/20/2016 9:31:03 AM
On 12/20/2016 3:28 AM, Nasser M. Abbasi wrote:
> On 12/20/2016 3:08 AM, Yufeng Xiong wrote:
>> Hi all, for instance, I have a matrix A:
>> [1   4;
>>  2   6;
>>  1   1;
>>  2   9;
>>  1   3;
>>  2   7;
>>  1   2;
>>  2   8;
>>  1   10;
>>  2   5]
>>
>> What I want is to find the maximal value in the second column wrt. the index in the first column:
>> [1  10;
>>  2   9]
>>
>
> one way might be
>
> B=unique(A,'rows');
> [B(find(diff(B(:,1))==1),:);B(end,:)]
>
> ans =
>
>       1    10
>       2     9
>
> This also should work
>
> B=sortrows(A);
> [B(find(diff(B(:,1))==1),:);B(end,:)]
>
> ps. did not debug it :)
>

Better like this

[B(find(diff(B(:,1))~=0),:);B(end,:)]

rather than what I had.

--Nasser



0
Nasser
12/20/2016 9:31:48 AM
Reply: