Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: Find the distance from an element to the matrix border

Say in Matlab I have a matrix like this:

1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9

I now need to find submatrixes 3x3 around every element (so each element in turn is the center of a 3x3 submatrix). When in the middle, it is no problem to find fx

1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9

which is the submatrix:

2 3 4
2 3 4
2 3 4

But when in the border of the matrix, that is elements in the first og last row or column, it of course isn't possible to find a 3x3 submatrix. Instead I need the submatrix that fits. In the corner fx I would get

1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9

where the submatrix is:

1 2
1 2

and in the middle of the border, I get fx:

1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9

which gives:

4 5 6
4 5 6

or as another example:

1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9

which gives:

6 7 8
6 7 8

I hope you understand my point. I am missing some feature that lets me find the distance from an element to the border.

I can treat every element as a center of a submatrix, and if I can just test, if the distance from the element to the border is below the submatrix's border (the submatrix dimension will namely change, to fx a 5x5 submatrix), then I can truncate a part of the submatrix while it is being made.

How do I find the distance from an element to the border of the matrix in the most efficient way?

like image 992
Steeven Avatar asked Jun 19 '12 08:06

Steeven


People also ask

How do you find the pairwise distance of a matrix in Matlab?

The pairwise distance between observations i and j is in D((i-1)*(m-i/2)+j-i) for i≤j. You can convert D into a symmetric matrix by using the squareform function. Z = squareform(D) returns an m-by-m matrix where Z(i,j) corresponds to the pairwise distance between observations i and j.

How do you find the distance between two points in a matrix?

This length can be computed with the help of Pythagora's theorem: dist = sqrt((x2-x1)^2 + (y2-y1)^2) . This is known as the Euclidian distance between the points.

How does Matlab calculate distance?

dist() can calculate the Euclidean distance of multiple points at once, it can certainly be used to calculate the distance for two points, although it seems to be an over-kill because the equation sqrt((x1-x2)^2+(y1-y2)^2) can do that too. Since the OP asked for a MATLAB function, I thought this is the one.


2 Answers

Though it doesn't answer your question directly, I guess that your problem in a higher context is to do some kind of filter or processing on the 3x3 elements. Usually the solution is to pad the array with extra values, say NaN. Then you can safely extract 3x3 sub-matrix, which might have some NaNs.

You can do the padding using padarray command.

Edit(1): I've read your comments to another answer. It looks like you are more interested in detecting the border cases, rather then computing the distance. If you want to know when you are close to a border, how about creating a mask?

mask = false(size(A));
mask(1:2,:) = true;
mask(end-1:end,:) = true;
mask(:,1:2) = true;
mask(:,end-1:end) = true;
like image 160
Andrey Rubshtein Avatar answered Sep 23 '22 19:09

Andrey Rubshtein


Let's specify the question a bit further: You have a matrix of the size nxm and you pick an element i,j. You want to know the distance of element i,j from the border? That would be: min(i,j,n-i,m-j)

like image 35
Bgs Avatar answered Sep 22 '22 19:09

Bgs