Matrix/Image Merging
Show older comments
Hi,
I have 2 matrix A and B which are different size, I would like to combine them together with A as the master to make a new matrix with consist of A and B. The way I want both matrix is merge through a location on both matrix. For example place Matrix A on Matrix B on certain location, Eg ‘8’ on Matrix A to Matrix B’s ‘22’ to result Matrix Merge.
A=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15]
A =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
B=[13 14 15 20 27 21; 16 17 18 19 25 26; 21 22 23 24 25 23]
B =
13 14 15 20 27 21
16 17 18 19 25 26
21 22 23 24 25 23
Merge=[0 13 14 15 20 27 21 ; 1 2 3 4 5 25 26 ; 6 7 8 9 10 25 23 ; 11 12 13 14 15 0 0]
Merge =
0 13 14 15 20 27 21
1 2 3 4 5 25 26
6 7 8 9 10 25 23
11 12 13 14 15 0 0
Besides that, additional space will have value of 0. I'll use these method to merge 2 images where i have a location at both images which going to be used to control the location of merging.
Thanks;
Accepted Answer
More Answers (2)
Matt Fig
on 24 Jun 2011
Kyle, what to do when multiple matches are found in B, since your example B has duplicates?
%
%
%
%
%
EDIT In response to clarification about duplicates.
Since you say there will not be duplicates in the actual data, I will use example matrices without duplicates:
A = reshape(1:12,3,4);
B = reshape(1:30,5,6)+12;
NA = 5; % The number to overlap in A.
NB = 35; % The number to overlap in B.
[mA,nA] = size(A);
[mB,nB] = size(B);
[IA,JA] = find(A==NA);
[IB,JB] = find(B==NB);
mC = mA+mB+mod(mA+mB,2)+1;
nC = nA+nB+mod(nA+nB,2)+1;
C = zeros(mC,nC);
cC = round([mC/2,nC/2]);
C(cC(1)-IB+1:cC(1)-IB+mB,cC(2)-JB+1:cC(2)-JB+nB) = B;
C(cC(1)-IA+1:cC(1)-IA+mA,cC(2)-JA+1:cC(2)-JA+nA) = A;
C = C(:,any(logical(C)));
C = C(any(logical(C),2),:)
7 Comments
Kyle
on 24 Jun 2011
Matt Fig
on 24 Jun 2011
But if there are two matches, then there is two ways to overlap. Which is preferred?
Kyle
on 24 Jun 2011
Sean de Wolski
on 24 Jun 2011
That would crop out black columns or rows of an image.
Matt Fig
on 24 Jun 2011
True, but Kyle said there were no duplicates. In that case there shouldn't be an entire row or column that is the same!
Sean de Wolski
on 24 Jun 2011
Good Point.
Kyle
on 25 Jun 2011
Kyle
on 25 Jun 2011
Categories
Find more on Creating and Concatenating Matrices 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!