finding the closest area match between matrix 3X3 and another data set

1 view (last 30 days)
HI, I hope this is more clear what I am trying to obtin:
At first I have my matrix A organized as grid with different cell-ids. size=950x1000;
I also have the matrix B with the same grid size filled with areas;
I also have array C with ids and array D with areas I want to find in A and B respectively. size [670x1] both of them
e.g. [A]-ids
2101 2102 2103 2104 etc
2201 2202 2203 2204
2301 etc
e.g. [B] -areas
25 102 104 105 etc
10 452 452 114
56 etc
so id 2202 has area of 452 for example and so on.
e.g. [C] -ids
2202
2207 etc
e.g. [D] -areas
100
52 etc
Then I intersect matrix A with array C to find position of cell-ids C in matrix A like:
[row,col]=find(ismember(A,C));
then I was looking for the neighbouring cells around those identified cells [row,col] as:
neighbors_id = [A(row, col),... A(row-1, col-1),... A(row-1, col),... A(row-1, col+1),... A(row, col-1),... A(row, col+1),... A(row+1, col-1),... A(row+1, col),... A(row+1, col+1)];
neighbors_area = [B(row, col),... B(row-1, col-1),... B(row-1, col),... B(row-1, col+1),... B(row, col-1),... B(row, col+1),... B(row+1, col-1),... B(row+1, col),... B(row+1, col+1)];
Then, what I want to do is to compare each of this 9 areas with D, find the closest value to D and extract its ID and value.
result: E should be size of 670 X 3
old_cel_id;new_id; area_new; area_D
2202; 2102; 102; 100
etc. using the same example as above.
ps. all the ids are inside so I do not have problem with grid edges.
  4 Comments
Jan
Jan on 7 Feb 2017
@sensation: Sorry, I'm totally lost. After "organized as grid with different cell-ids" I'm not sure if I can follow you anymore. "Grid"? "cell-id"? How are "areas" stored in D?
sensation
sensation on 7 Feb 2017
in my case grid means 950 rows X 1000 columns and every [row,col] has its either id (in A) or its corresponding area (in B). Areas in D correspond to each id in C (so first value of area D(1,1) has its id in C(1,1) and so on. Maybe if it is possible I can send it to you my input data so you can have quick look? Thanks

Sign in to comment.

Answers (1)

Jan
Jan on 7 Feb 2017
A bold guess:
B = randi([1, 100], 540, 4860); % Test data
A = randi([1, 100], 540, 1);
mix = [2,5,7,3,1,8,4,6,9];
Result = zeros(540, 540, 540); % Perhaps, this is not clear
C = permute(reshape(B, 540, 9, 540), [2, 1, 3]);
for iA = 1:540
for iB1 = 1:540
for iB2 = 1:540
[minValue, minIndex] = min(abs(A(iA) - C(:, iB1, iB2)));
Result(iB1, iB2, iA) = mix(minIndex);
end
end
end
If this does, what you need, a vectorization will improve the speed. But before caring about the speed, we have to clarify at first, what you exactly want to calculate.
  1 Comment
sensation
sensation on 8 Feb 2017
Hi Jan, I have just founded an error in my matrix result. So lets say I have 10 rows and columns obtained by matching between two data sets: [row,col]=find(ismember(A,C));
My question is how I can get the neighbouring cells around those 10 central cells obtained with previous equation?
Up to now my code is duplicating the values, i.e. it takes row=1 and col 1 first, then row=1 and col=2, then row=1 and col=3 etc. What I need is to have iteration only around central ids. row=1 and col=1, then row=2 and col=2 etc; o finally i could have my results array with 10 rows (because we have ten central cells) and 9 columns (corresponding to neighbouring cells including the central one).
thanks a lot

Sign in to comment.

Categories

Find more on Data Preprocessing in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!