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:
How to find the index of a know element in a matrix?

Subject: How to find the index of a know element in a matrix?

From: Diogo Londero da Silva

Date: 3 Nov, 2010 11:26:04

Message: 1 of 2

Hi,

I would like to compare the elements of two diferent matrixes (A and B), related by the first colum, that in my case represents the time. The matrixes always have the same number of columns but different numbers of rows. In adittion, the numbers in the frist column are close, but not identical. For example:

A=[ 1 2
5 4
7 6]

B=[1.5 2.1
3.2 3.1
4.9 4.1
6.7 7]

To compare the second row of matrix A ( A(2,1)=5) , how can I find the index of the correspondent time in matrix B? In this case, the answere will be B(3,1)=4.9, which is the closest number to 5.

Many thanks, Diogo.

Subject: How to find the index of a know element in a matrix?

From: Sean

Date: 3 Nov, 2010 12:30:10

Message: 2 of 2

"Diogo Londero da Silva" <diogolondero@yahoo.com.br> wrote in message <iargsc$8fv$1@fred.mathworks.com>...
> Hi,
>
> I would like to compare the elements of two diferent matrixes (A and B), related by the first colum, that in my case represents the time. The matrixes always have the same number of columns but different numbers of rows. In adittion, the numbers in the frist column are close, but not identical. For example:
>
> A=[ 1 2
> 5 4
> 7 6]
>
> B=[1.5 2.1
> 3.2 3.1
> 4.9 4.1
> 6.7 7]
>
> To compare the second row of matrix A ( A(2,1)=5) , how can I find the index of the correspondent time in matrix B? In this case, the answere will be B(3,1)=4.9, which is the closest number to 5.
>
> Many thanks, Diogo.

So for every element in A, you want to know the closest element in B?

%Data
A=[ 1 2;5 4 ;7 6]
B=[1.5 2.1
 3.2 3.1
 4.9 4.1
 6.7 7]

%Engine
[~,idx] = min(bsxfun(@(x,y)(abs(x-y)),A(:),B(:).'),[],2);
reshape(B(idx),size(A))

Tags for this Thread

No tags are associated with 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