![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/883825/image.png)
How to make summation of moved and fixed matrix ?
2 次查看(过去 30 天)
显示 更早的评论
I would like to calculate the summation of moved and fixed matrix.
The purpose is to join two image without the spaces(zeros) between.
TEMP is the matrix and DATA is the moved matrix by LengthofZero.
To calculate the summation of different sizes matrix, I made the zeros to make the same size as below.
The below is the code I made, but I think there is more efficient ways available in Matlab.
Please help me please.
load('A1.mat')
load('A2.mat')
figure(1)
imshowpair(A1,A2,'montage')
TEMP = A1; DATA = A2;
LengthofZero = 251;
% Move the data in the column direction by LengthofZero and summation of DATA & TEMP
[m1,n1] = size(TEMP);
[m2,n2] = size(DATA);
TEMP = [TEMP,zeros(m2,n2)];
DATA = [zeros(m1,n1),DATA];
DATA = circshift(DATA,-LengthofZero,2);
TEMP = TEMP + DATA;
figure(2)
imshow(TEMP,[])
0 个评论
采纳的回答
Image Analyst
2022-2-4
Is this what you mean:
load('A1.mat')
load('A2.mat')
subplot(2, 2, 1);
imshow(A1, []);
axis('image', 'on')
subplot(2, 2, 2);
imshow(A2, []);
axis('image', 'on')
[rows1, columns1] = size(A1);
[rows2, columns2] = size(A2);
% Make a canvass to hold the two images.
TEMP = [A1, zeros(size(A2))];
for row = 1 : rows1
% Find the last non-zero column in A1
col1 = find(A1(row, :), 1, 'last');
% Find the first non-zero column in A2
col2 = find(A2(row, :), 1, 'first');
% Extract the pixels from A2 that we need to paste onto TEMP
rowFromA2 = A2(row, col2:end);
% Get the length of the part we're going to paste
numColumns = length(rowFromA2);
% Paste that row from A2 from that column to the right.
TEMP(row, col1:col1 + numColumns - 1) = rowFromA2;
end
% Get rid of any columns in TEMP that are all zeros
allZeroColumns = all(TEMP == 0, 1);
TEMP(:, allZeroColumns) = []
% Display final image.
subplot(2, 2, 3:4);
imshow(TEMP, []);
axis('image', 'on')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/883825/image.png)
2 个评论
Image Analyst
2022-2-4
circshift() will not slide the second piece up to the right side of the left piece like a jigsaw puzzle. There is no built-in function to do this automatically - you have to do it by using lower level functions like I did.
How big is your array? This should be pretty fast unless your matrices are like gigabytes in size, but for a few thousand by a few thousand matrix it should be pretty quick. If you have millions of rows it will of course take longer.
更多回答(1 个)
Simon Chan
2022-2-4
Not sure it is efficient or not, just an optional way of doing it:
load('A1.mat');
load('A2.mat');
s1 = regionprops(bwareafilt(~A1,2),'BoundingBox'); % Use complement image and remove the smallest object
bbox1 = cat(1,s1.BoundingBox); % Find the bounding boxes coordinates
B1 = A1(:,1:floor(max(bbox1(:,1)))); % First image get the largest starting point of the Bounding Box
s2 = regionprops(bwareafilt(~A2,2),'BoundingBox');
bbox2 = cat(1,s2.BoundingBox);
B2 = A2(:,ceil(min(bbox2(:,1)+bbox2(:,3))):end); % Second image get the smallest ending point of the Bounding Box
B = [B1,B2]; % Combine them
imshow(B,[])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/883845/image.jpeg)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!