Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Finding indices

Subject: Finding indices

From: matthew mccarthy

Date: 27 Apr, 2009 08:59:02

Message: 1 of 7

I am having trouble with part of a project and need some help.
Lets just say i have a matrix m=[1,3,5;4,5,6]
I need to find the row and column where the matrix equals 5 first. The answer in this case would be row 2 column 2, but i cannot use the find function to do this.

Ive tried using [r,c]=m==5 but it says too many output arguments.

Thanks for the help.

Subject: Finding indices

From: Joerg Buchholz

Date: 27 Apr, 2009 09:30:04

Message: 2 of 7

"matthew mccarthy" <batter8@aol.com> wrote in message <gt3s4m$cfv$1@fred.mathworks.com>...
> I am having trouble with part of a project and need some help.
> Lets just say i have a matrix m=[1,3,5;4,5,6]
> I need to find the row and column where the matrix equals 5 first. The answer in this case would be row 2 column 2, but i cannot use the find function to do this.
>
> Ive tried using [r,c]=m==5 but it says too many output arguments.
>
> Thanks for the help.

What is wrong with

[r, c] = find (m == 5)

Joerg

Subject: Finding indices

From: Jos

Date: 27 Apr, 2009 10:26:03

Message: 3 of 7

"matthew mccarthy" <batter8@aol.com> wrote in message <gt3s4m$cfv$1@fred.mathworks.com>...
> I am having trouble with part of a project and need some help.
> Lets just say i have a matrix m=[1,3,5;4,5,6]
> I need to find the row and column where the matrix equals 5 first. The answer in this case would be row 2 column 2, but i cannot use the find function to do this.
>
> Ive tried using [r,c]=m==5 but it says too many output arguments.
>
> Thanks for the help.

Why can't you use FIND

[r,c] = find(m==5)
[minC,idx] = min(c)
minR = r(idx)

without FIND you can use logical indexing

[r,c] = meshgrid(1:size(m,1),1:size(m,2)) ;
q = m == 5 ;
r = r(q)
c = c(q)
% etc.

Jos

Subject: Finding indices

From: Ahmad

Date: 27 Apr, 2009 10:33:01

Message: 4 of 7

"Joerg Buchholz" <buchholz@hs-bremen.de> wrote in message <gt3tus$io7$1@fred.mathworks.com>...
> "matthew mccarthy" <batter8@aol.com> wrote in message <gt3s4m$cfv$1@fred.mathworks.com>...
> > I am having trouble with part of a project and need some help.
> > Lets just say i have a matrix m=[1,3,5;4,5,6]
> > I need to find the row and column where the matrix equals 5 first. The answer in this case would be row 2 column 2, but i cannot use the find function to do this.
> >
> > Ive tried using [r,c]=m==5 but it says too many output arguments.
> >
> > Thanks for the help.
>
> What is wrong with
>
> [r, c] = find (m == 5)
>
> Joerg

Just to add to this .... if you want to find the first 5 ... you can use find() like this:
find(m==5, 1, 'first')

Subject: Finding indices

From: matthew mccarthy

Date: 27 Apr, 2009 19:52:01

Message: 5 of 7

"matthew mccarthy" <batter8@aol.com> wrote in message <gt3s4m$cfv$1@fred.mathworks.com>...
> I am having trouble with part of a project and need some help.
> Lets just say i have a matrix m=[1,3,5;4,5,6]
> I need to find the row and column where the matrix equals 5 first. The answer in this case would be row 2 column 2, but i cannot use the find function to do this.
>
> Ive tried using [r,c]=m==5 but it says too many output arguments.
>
> Thanks for the help.
Well its for a project and it states that we are not allowed to use the find function or basically any other functions and implicit loops.
I think its because we just learned the if, for, and while loops and she wants us to use them

Subject: Finding indices

From: Matt Fig

Date: 27 Apr, 2009 20:03:01

Message: 6 of 7

"matthew mccarthy" <batter8@aol.com> wrote in message <gt3s4m$cfv$1@fred.mathworks.com>...
> I am having trouble with part of a project and need some help.
> Lets just say i have a matrix m=[1,3,5;4,5,6]
> I need to find the row and column where the matrix equals 5 first. The answer in this case would be row 2 column 2, but i cannot use the find function to do this.
>
> Ive tried using [r,c]=m==5 but it says too many output arguments.
>
> Thanks for the help.

In part we need to know what YOU mean by "equals 5 first." Do you count rows first or columns first? For example, where is the first 5 in [1 5;5 1]? Once you define that, you can use or modify this horrible construct:


m=[1,3,5;4,5,6]

for ii = 1:2 % I assume you cannot use size either?
    for jj = 1:3
        if m(ii,jj)==5
            r = ii;
            c = jj;
        end
    end
end

Subject: Finding indices

From: matthew mccarthy

Date: 27 Apr, 2009 21:45:03

Message: 7 of 7

well we already have the dimensions i just made up a problem for the sake of me not having to type out the whole project.
I just read some more instructions and it says we have to perform a row-wise search.
So we look at the first row first and if the value is not found then we got to the next row and so on.
So actually the answer would be row 1 column 3.
Ive been trying to figure this out and i just have no clue

Tags for this Thread

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.

Contact us