views:

98

answers:

2

I know the center (x,y) coordinates of a subarray in terms of the subarray space and general array. For other parts of the subarray I also know the coordinates in the subarray space - but I want to find the coordinates in the general array? Is there an elegant way to do it in MATLAB? In principle I think you should still be able to find where it would be in the array space.

For example, let's say (32,18) are the (x,y) coordinates of an element. Then you have a small 8x8 subarray that includes the same element. Let's say the coordinates for the same element in the subarray space are (3,5). What would be the coordinates in the larger array for something that is, for example, (6.2,7.1) in the subarray?

A: 

Following your example, let's take an array of 100x100 and use element (32,18) as our point of interest. In MATLAB, you can use the colon operator to access subarrays.

array=rand(100);
x=32;
y=18;
subdim=8;
subx=3;
suby=5;

Let's build a subarray of 64 elements with your point at (3,5).

subarray=array(x-(subx-1):x+(subdim-subx),y-(suby-1):y+(subdim-suby));

As you can see, now subarray(3,5)==array(x,y);

So if you want to find the original array indices for subarray element (6,7):

X=x-subx+6;
Y=y-suby+7;

which gives

array(X,Y)==subarray(6,7);

Or, if you just want to find where element (6,7) of the subarray is in the original array, you can use the find function. (Beware, if you have duplicate elements in the array, it will find all of them.)

[x,y]=find(array==subarray(6,7));

Doresoom
A: 

If you have an m-by-n array A, and you are mapping coordinate point (x1,y1) to the center of element A(1,1) and coordinate point (x2,y2) to the center of element A(m,n), then here's a general way to find the index value of the array element whose center is closest to a given coordinate value (x3,y3):

sizeA = size(A);  %# The row and column sizes of A
p1 = [x1 y1];     %# Point for A(1,1)
p2 = [x2 y2];     %# Point for A(m,n)
p3 = [x3 y3];     %# Point to find indices for

indices = round((sizeA-1).*(p3-p1)./(p2-p1))+1;  %# Get the raw indices
indices = min(max(indices,[1 1]),sizeA);  %# Limit the indices to the array size
gnovice