COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### need help with distance of coordinates

• Follow

```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....

```
 0

```"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
```
 0

```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.
```
 0

```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
```
 0

```"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
```
 0

```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);
```
 0

```Matt - you're right.
```
 0

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

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

```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...
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~~~
```
 0

```"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...
> 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~
```
 0

9 Replies
169 Views

Similiar Articles:

7/22/2012 8:32:51 PM