Thread Subject: finding the row of a particular number in a matrix

Subject: finding the row of a particular number in a matrix

From: Pia

Date: 26 Oct, 2009 12:01:05

Message: 1 of 5

Hi,
I have a matrix and I find the max value in particular column and then I want to know what row that number is in. Could someone please advise on how to do this?
Thanks,
Pia

Subject: finding the row of a particular number in a matrix

From: the cyclist

Date: 26 Oct, 2009 12:44:18

Message: 2 of 5

"Pia " <p.sartor@sheffield.ac.uk> wrote in message <hc4320$o4$1@fred.mathworks.com>...
> Hi,
> I have a matrix and I find the max value in particular column and then I want to know what row that number is in. Could someone please advise on how to do this?
> Thanks,
> Pia

My first piece of advice is that you should carefully read

>> doc max

which shows you how to call the function MAX with two output arguments, one of which gives the location of the maximum. If you cannot figure it out from that, then I suggest you post the code you have so far, so that we can provide better guidance.

Subject: finding the row of a particular number in a matrix

From: Matt Fetterman

Date: 26 Oct, 2009 16:08:03

Message: 3 of 5

"the cyclist" <thecyclist@gmail.com> wrote in message <hc45j2$9o5$1@fred.mathworks.com>...
> "Pia " <p.sartor@sheffield.ac.uk> wrote in message <hc4320$o4$1@fred.mathworks.com>...
> > Hi,
> > I have a matrix and I find the max value in particular column and then I want to know what row that number is in. Could someone please advise on how to do this?
> > Thanks,
> > Pia
>
> My first piece of advice is that you should carefully read
>
> >> doc max
>
> which shows you how to call the function MAX with two output arguments, one of which gives the location of the maximum. If you cannot figure it out from that, then I suggest you post the code you have so far, so that we can provide better guidance.

Here is one way, although maybe not the most elegant. But it does scale nicely to N-dimensions.

h=rand(5,5); % this will be the matrix that we want to find the max.
P=reshape(h,numel(h) ,1); % convert h into 1D matrix.
[yoki,Aindex]=max(P); % find index corresponding to max point in 1D
[xIndex,yIndex]=ind2sub(size(h),Aindex); % find 2D indices of max.

Subject: finding the row of a particular number in a matrix

From: Nasser M. Abbasi

Date: 26 Oct, 2009 20:24:54

Message: 4 of 5


"Matt Fetterman" <mattinjersey@yahoo.com> wrote in message
news:hc4hh3$f6d$1@fred.mathworks.com...
> "the cyclist" <thecyclist@gmail.com> wrote in message
> <hc45j2$9o5$1@fred.mathworks.com>...
>> "Pia " <p.sartor@sheffield.ac.uk> wrote in message
>> <hc4320$o4$1@fred.mathworks.com>...
>> > Hi,
>> > I have a matrix and I find the max value in particular column and then
>> > I want to know what row that number is in. Could someone please advise
>> > on how to do this?
>> > Thanks,
>> > Pia
>>

>> My first piece of advice is that you should carefully read
>>
>> >> doc max
>>
>> which shows you how to call the function MAX with two output arguments,
>> one of which gives the location of the maximum. If you cannot figure it
>> out from that, then I suggest you post the code you have so far, so that
>> we can provide better guidance.
>


> Here is one way, although maybe not the most elegant. But it does scale
> nicely to N-dimensions.
>
> h=rand(5,5); % this will be the matrix that we want to find the max.
> P=reshape(h,numel(h) ,1); % convert h into 1D matrix.
> [yoki,Aindex]=max(P); % find index corresponding to max point in 1D
> [xIndex,yIndex]=ind2sub(size(h),Aindex); % find 2D indices of max.

But would it not be a bit simpler to use max as suggested and take advatange
of its return values as follows?

h=rand(5,5)
[val,c]=max(h,[],1);
[val,r]=max(val);

fprintf('Max is at row=%d, column=%d\n',c(r),r)

--Nasser

Subject: finding the row of a particular number in a matrix

From: Nasser M. Abbasi

Date: 26 Oct, 2009 21:34:07

Message: 5 of 5


"Nasser M. Abbasi" <nma@12000.org> wrote in message
news:qAnFm.32582$eJ4.26608@newsfe07.iad...
>
> "Matt Fetterman" <mattinjersey@yahoo.com> wrote in message
> news:hc4hh3$f6d$1@fred.mathworks.com...
>> "the cyclist" <thecyclist@gmail.com> wrote in message
>> <hc45j2$9o5$1@fred.mathworks.com>...
>>> "Pia " <p.sartor@sheffield.ac.uk> wrote in message
>>> <hc4320$o4$1@fred.mathworks.com>...
>>> > Hi,
>>> > I have a matrix and I find the max value in particular column and then
>>> > I want to know what row that number is in. Could someone please
>>> > advise on how to do this?
>>> > Thanks,
>>> > Pia
>>>
>
>>> My first piece of advice is that you should carefully read
>>>
>>> >> doc max
>>>
>>> which shows you how to call the function MAX with two output arguments,
>>> one of which gives the location of the maximum. If you cannot figure it
>>> out from that, then I suggest you post the code you have so far, so that
>>> we can provide better guidance.
>>
>
>
>> Here is one way, although maybe not the most elegant. But it does scale
>> nicely to N-dimensions.
>>
>> h=rand(5,5); % this will be the matrix that we want to find the max.
>> P=reshape(h,numel(h) ,1); % convert h into 1D matrix.
>> [yoki,Aindex]=max(P); % find index corresponding to max point in 1D
>> [xIndex,yIndex]=ind2sub(size(h),Aindex); % find 2D indices of max.
>

> But would it not be a bit simpler to use max as suggested and take
> advatange of its return values as follows?
>
> h=rand(5,5)
> [val,c]=max(h,[],1);
> [val,r]=max(val);
>
> fprintf('Max is at row=%d, column=%d\n',c(r),r)
>
> --Nasser

Another way, which may be even better (finds all positions of the maximum
value)

[r,c]=find(h==max(max(h)))

--Nasser

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com