finding the closest area match between matrix 3X3 and another data set
1 view (last 30 days)
Show older comments
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
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?
Answers (1)
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.
See Also
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!