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:
getting index of matrix

Subject: getting index of matrix

From: Ashwini Deshpande

Date: 12 Mar, 2009 07:04:06

Message: 1 of 5

Hi,

I have 2 matrices say ref and data. I have to get a number from user and that has to be matched with the element of the matrix ref and corresponding element from data matrix has to be read and displayed.

For example:
ref = [1 2 3 4 5 6 7 8 9 10];
data = [11 25 65 87 23 12 45 78 23 56];

num = input('Enter Number: ');

num = 2.3;

then,

get index of the element in ref matrx i,e., round(num) = 2;
index is 2.

Now read 2nd element of data i.e., 25
and display it.

I am getting stuck at matching the round off value of num with ref mtrix..

How do i do it ... Can anyone help me plz ..

Thanks,
Ashwini !

Subject: getting index of matrix

From: Roger Stafford

Date: 12 Mar, 2009 07:44:03

Message: 2 of 5

"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in message <gpac56$ier$1@fred.mathworks.com>...
> Hi,
>
> I have 2 matrices say ref and data. I have to get a number from user and that has to be matched with the element of the matrix ref and corresponding element from data matrix has to be read and displayed.
>
> For example:
> ref = [1 2 3 4 5 6 7 8 9 10];
> data = [11 25 65 87 23 12 45 78 23 56];
>
> num = input('Enter Number: ');
>
> num = 2.3;
>
> then,
>
> get index of the element in ref matrx i,e., round(num) = 2;
> index is 2.
>
> Now read 2nd element of data i.e., 25
> and display it.
>
> I am getting stuck at matching the round off value of num with ref mtrix..
>
> How do i do it ... Can anyone help me plz ..
>
> Thanks,
> Ashwini !

  display(data(ref==round(num)))

Roger Stafford

Subject: getting index of matrix

From: Jos

Date: 12 Mar, 2009 07:44:03

Message: 3 of 5

"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in message <gpac56$ier$1@fred.mathworks.com>...
> Hi,
>
> I have 2 matrices say ref and data. I have to get a number from user and that has to be matched with the element of the matrix ref and corresponding element from data matrix has to be read and displayed.
>
> For example:
> ref = [1 2 3 4 5 6 7 8 9 10];
> data = [11 25 65 87 23 12 45 78 23 56];
>
> num = input('Enter Number: ');
>
> num = 2.3;
>
> then,
>
> get index of the element in ref matrx i,e., round(num) = 2;
> index is 2.
>
> Now read 2nd element of data i.e., 25
> and display it.
>
> I am getting stuck at matching the round off value of num with ref mtrix..
>
> How do i do it ... Can anyone help me plz ..
>
> Thanks,
> Ashwini !

There are a couple of ways to find the index number:

idx1 = find(ref == round(num)) % may not return exactly 1 element
[val, idx2] = min(abs(ref-num))
idx3 = interp1(ref,1:numel(ref),num,'nearest')

and then use it to index into "data"

Result = data(idx2)

hth
Jos

Subject: getting index of matrix

From: Ashwini Deshpande

Date: 12 Mar, 2009 08:23:52

Message: 4 of 5

Thats great ...

Both methods are working fine ...

Thank u very much :-)

Ashwini

Subject: getting index of matrix

From: Soraya

Date: 18 Jun, 2011 19:12:04

Message: 5 of 5

"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in message <gpagqo$b8e$1@fred.mathworks.com>...
> Thats great ...
>
> Both methods are working fine ...
>
> Thank u very much :-)
>
> Ashwini

Thank you for the post and the solutions in details. I also used the methods to solve my problem.

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