How to merge two matrixes by position vector?

5 ビュー (過去 30 日間)
Lisa Rademacher
Lisa Rademacher 2021 年 5 月 2 日
編集済み: Lisa Rademacher 2021 年 5 月 5 日
Hey everybody,
I have two matrixes each having a x- and y-position vector, looking like this:
x1 = [1.10, 2.11, 3.07, 4.10, 5.09, 6.10, 7.12, 8.10];
x2 = [5.45, 6.43, 7.46, 8.45, 9.44, 10.42, 11.43];
y1 = [-4.30, -3.29, -2.35, -1.31, 0.30, 1.28, 2.32, 3.30];
y2 = [-3.70, -2.69, -1.67, 0.71, 1.70, 2.72, 3.71, 4.68];
A1 = randi(10, length(x1), length(y1));
A2 = randi(10, length(x2), length(y2));
The way I was doing it until now:
  1. I was looking for the x-overlap, assuming same resolution (diff(x1) is approximately diff(x2))
diff = abs(x1 - x2);
x_idx = find(diff == min(diff));
overlap_x = x_idx : length(x1); %(A1 is normally at front position)
2. comparing the length of y1 and y2, assuming that they nearly show the same positions
if length(y1) > length(y2), y_final = length(y1);
else, y_final = length(y2);
end
3. and created a new matrix with A1 and A2 with both overlapping at their position
m_final = zeros(length(x1) + length(x2) - length(overlap_x), y_final);
%filling in A1
m_final(1:length(x1), 1:length(y1)) = A1;
%overlapping area is averaged
m_final(overlap_x , 1:length(y2)) = (m_final(overlap_x , 1:length(y2)) + A2(1:length(overlap_x), 1:length(y2)))/2;
%remaining part
m_final(overlap_x(end)+1 : x_idx+length(x2) -1, 1:length(y2) ) = ...
A2(length(overlap_x)+1:length(x2) , 1:length(y2));
But this is not quite accurate, the y2-vector can picture a position of matrix A2 which is a bit lower or higher or wider than A1. I tried to handle it with different cases (y2(1)<y1(1) and 2(end)<y1(end)) but ended up in a terrible mess. So my question is now, which (easy and clear ^^) approach would you propose to handle the y-position and the overlapping area so that the outcome could look like the attached picture? I also added some example data. (It already looks a bit like the picture downstream, but I need this approach to combine a lot of those matrices together.)

回答 (1 件)

Benjamin Großmann
Benjamin Großmann 2021 年 5 月 3 日
Maybe this works. Hard to tell because the randi images are not so comparable to your image.
Basic idea is to combine the coordinates x1 and x2 into x as well as y1 and y2 into y. Then sort x and sort y and find the values of x1,x2,y1,y2 in x and y to get indices. These indices should correspond to the place where A1 and A2 have to be in the merged matrix A.
clearvars
close all
clc
%% Definitions
x1 = [1.10, 2.11, 3.07, 4.10, 5.09, 6.10, 7.12, 8.10];
x2 = [5.45, 6.43, 7.46, 8.45, 9.44, 10.42, 11.43];
y1 = [-4.30, -3.29, -2.35, -1.31, 0.30, 1.28, 2.32, 3.30];
y2 = [-3.70, -2.69, -1.67, 0.71, 1.70, 2.72, 3.71, 4.68];
A1 = randi(10, length(x1), length(y1));
A2 = randi(10, length(x2), length(y2));
% Initialize the merged matrix with NaN
A = NaN(numel(y1) + numel(y2), numel(x1) + numel(x2));
% combine x1, x2
x = [x1 x2];
% combine y1, y2
y = [y1 y2];
% sort the combined coordinates
x = sort(x);
y = sort(y);
% Use ismember to get the indices of x1 in x
[~, A1_x] = ismember(x1, x);
% Use ismember to get the indices of y1 in y
[~, A1_y] = ismember(y1, y);
% Use ismember to get the indices of x2 in x
[~, A2_x] = ismember(x2, x);
% Use ismember to get the indices of y2 in y
[~, A2_y] = ismember(y2, y);
% Write A1 at the corresponding position in A
A(A1_x, A1_y) = A1;
% Write A2 at the corresponding position in A
A(A2_x, A2_y) = A2;
figure
imagesc(x1, y1, A1)
figure
imagesc(x2, y2, A2)
figure
imagesc(x, y, A)
Another approuch would be to define a common mesh and use interp2 to place the two pictures onto these mesh or to use imfuse if you have the Image Processing Toolbox.
  1 件のコメント
Liz Lala
Liz Lala 2021 年 5 月 5 日
I really like the idea to use sort to combine both position vectors, but it gives a lot of NAN-rows in final matrix A messing up the plot in the end. Also i think the matrices are not always in agreement, so I did a (weightend) averaging in the overlapping area to create somethig like a crossfade. The weightening part was skipped in my question. I will add an example matrix pair in my question. thanks for your help =)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by