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:
need help with distance of coordinates

Subject: need help with distance of coordinates

From: si yeol Shin

Date: 24 Nov, 2010 10:21:04

Message: 1 of 10

Hello everybody
i need to calculate distances of coordinates.
for example

x1=[1 2 3]
y1=[1 2 3]
and
x2=[2 4 6 8]
y2=[2 4 6 8]

(much more values in original data)

then i want to make matrix as below

       1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3
2,2 v v v v v v v v v
2,4 v v v v v v v v v
2,6 v v v v v v v v v
2,8 v v v v v v v v v
4,2 v v v v v v v v v
  . . . . . . . . . .
  . . . . . . . . . .
8,8 v v v v v v v v v

v= values calculated
and i have to remove specific values
for example below 20.

and the result could be like this

       1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3
2,2 v v v v v v v v v
2,4 v v v v v v v v
2,6 v v v v v v v
2,8 v v v v v v
4,2 v v v v v
  . . . . . .
  . . . . . .
8,8 v v v v

and you'd better to see this post by view original format.
plz help me....
 

Subject: need help with distance of coordinates

From: Roger Stafford

Date: 24 Nov, 2010 18:26:04

Message: 2 of 10

"si yeol Shin" <siyory@kookmin.ac.kr> wrote in message <icioug$2l4$1@fred.mathworks.com>...
> ......
> 1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3
> 2,2 v v v v v v v v v
> ......
 - - - - - - - -
  You haven't explained what you mean by "distance of coordinates" which I assume are the 'v' values you refer to. Please give us a few computed examples of these v values. Also you haven't stated how you want the final result arranged. Since it isn't necessary a rectangle, it can't be a two-dimensional array. Do you want a single vector and if so in what order?

Roger Stafford

Subject: need help with distance of coordinates

From: si yeol Shin

Date: 25 Nov, 2010 00:50:04

Message: 3 of 10

ok. it was my mistake i didn't give you enough explain.
at first the values 'v' is the distance between two points on a coordinate.
so i put one set of x1,y1 in rows and another x2,y2 are putted in columns.
it doesn't matter if it could be calculated and made 16x9 matrix ,the x1,y1 can go on a cell together or two cells seperate.
and i want to calculate all of the points one by one.
so it will be given 144 values at all.
the formula is ' v=sqrt((x2-x1)^2+(y2-y1)^2).
at last i want remove specific values (ex, below 2) from the array that contains values.
thank you.

Subject: need help with distance of coordinates

From: ImageAnalyst

Date: 25 Nov, 2010 02:57:47

Message: 4 of 10

So the obvious way is to just put your formula into a group of 4
nested for loops. There may be some more efficient way but for less
than a few million numbers in your matrix, I doubt you'd notice any
difference.

x1=[1 2 3]
y1=[1 2 3]
x2=[2 4 6 8]
y2=[2 4 6 8]

row = 1;
col = 1;
for xx1 = x1
for yy1 = y1
for xx2=x2
for yy2 = y2
row = row+1;
distance(row, col) = sqrt((xx2-xx1)^2+(yy2-yy1)^2);
fprintf(1, 'From (%d, %d) to (%d, %d) = %.2f\n', ...
xx1,yy1,xx2,yy2, distance(row, col));
if row == length(x2)*length(y2)
row = 0;
col = col+1;
end
end
end
end
row = 0;
end
% Display the final matrix.
distance


There is no way to remove values from the matrix. A rectangular
matrix must have values in every location. If you really want to do
that you'll have to abandon using a numerical matrix and switch to
using a cell array. It will still have locations but the cells at
those locations could contain a null if you want. Or maybe you just
want to do logical indexing, like in the FAQ:
http://matlab.wikia.com/wiki/FAQ#How_does_logical_indexing_work.3F

Subject: need help with distance of coordinates

From: Roger Stafford

Date: 25 Nov, 2010 03:59:06

Message: 5 of 10

"si yeol Shin" <siyory@kookmin.ac.kr> wrote in message <ickbrs$qhd$1@fred.mathworks.com>...
> ok. it was my mistake i didn't give you enough explain.
> at first the values 'v' is the distance between two points on a coordinate.
> so i put one set of x1,y1 in rows and another x2,y2 are putted in columns.
> it doesn't matter if it could be calculated and made 16x9 matrix ,the x1,y1 can go on a cell together or two cells seperate.
> and i want to calculate all of the points one by one.
> so it will be given 144 values at all.
> the formula is ' v=sqrt((x2-x1)^2+(y2-y1)^2).
> at last i want remove specific values (ex, below 2) from the array that contains values.
> thank you.
- - - - - - - - -
  If I interpret what you have said correctly, the following should accomplish what you are seeking.

 [Y2,X2,Y1,X1] = ndgrid(y2,x2,y1,x1);
 M1 = sqrt((X2-X1).^2+(Y2-Y1).^2);
 M1 = reshape(M1,[length(y2)*length(x2),length(y1)*length(x1)]);

M1 should be the two-dimensional first result. The points involved in the distance values are implicit in their position within M1.

  To remove values as in your example of those below 20, do this:

 M2 = M1(M1>=20);

M2 will be a one-dimensional vector.

  The trouble with interpreting M2 is that you will have difficulty associating the distance values with the points they correspond to if that is needed, unless you do something similar to the above with the four indices for the points' coordinates.

Roger Stafford

Subject: need help with distance of coordinates

From: Matt Fig

Date: 25 Nov, 2010 04:30:05

Message: 6 of 10

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <a7006b27-31ef-4743-a746-c158340112e5@g19g2000yqg.googlegroups.com>...
> So the obvious way is to just put your formula into a group of 4
> nested for loops. There may be some more efficient way but for less
> than a few million numbers in your matrix, I doubt you'd notice any
> difference.
>
> x1=[1 2 3]
> y1=[1 2 3]
> x2=[2 4 6 8]
> y2=[2 4 6 8]
>
> row = 1;
> col = 1;
> for xx1 = x1
> for yy1 = y1
> for xx2=x2
> for yy2 = y2
> row = row+1;
> distance(row, col) = sqrt((xx2-xx1)^2+(yy2-yy1)^2);
> fprintf(1, 'From (%d, %d) to (%d, %d) = %.2f\n', ...
> xx1,yy1,xx2,yy2, distance(row, col));
> if row == length(x2)*length(y2)
> row = 0;
> col = col+1;
> end
> end
> end
> end
> row = 0;
> end
> % Display the final matrix.
> distance


IA, I believe you need to start with row = 0, or move the increment to a place after the distance calculation.

To the OP, if Roger's code causes a memory problem, you should use the IA's code (with the change mentioned above), and don't forget to Pre-allocate the distance matrix. I say this because you said that your actual data was much larger than the example. I.e.,

distance = zeros(length(x2)^2*length(x1)^2);

Subject: need help with distance of coordinates

From: ImageAnalyst

Date: 25 Nov, 2010 04:44:29

Message: 7 of 10

Matt - you're right.

Subject: need help with distance of coordinates

From: Matt Fig

Date: 25 Nov, 2010 04:50:05

Message: 8 of 10

"Matt Fig" <spamanon@yahoo.com> wrote in message
> distance = zeros(length(x2)^2*length(x1)^2);

There is a bad typo there! It should read:

distance = zeros(length(x2)^2,length(x1)^2);

Subject: need help with distance of coordinates

From: si yeol Shin

Date: 25 Nov, 2010 10:33:04

Message: 9 of 10

thanks alot~ it was very helpful for me.
and i've another question.
you told that it is difficult to remove the specific values what i want...
it's sounds sad...
is there a way to translate the specific values to NaN ?
i know it is able to calculate the mean of 1 columns values with Nan using 'nanmean'
but i couldn't find a way how to calculate 1columns values divide by mean.
the result was Nan because the column contains Nan.
how to do this?
thank you everyone~~~

Subject: need help with distance of coordinates

From: si yeol Shin

Date: 25 Nov, 2010 10:49:05

Message: 10 of 10

"si yeol Shin" <siyory@kookmin.ac.kr> wrote in message <icle10$djc$1@fred.mathworks.com>...
> thanks alot~ it was very helpful for me.
> and i've another question.
> you told that it is difficult to remove the specific values what i want...
> it's sounds sad...
> is there a way to translate the specific values to NaN ?
> i know it is able to calculate the mean of 1 columns values with Nan using 'nanmean'
> but i couldn't find a way how to calculate 1columns values divide by mean.
> the result was Nan because the column contains Nan.
> how to do this?
> thank you everyone~~~

the problem about division is solved~

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