How can I calculate the difference between two matrix with different size?

Hi everyone,
I have a problem in calculating the difference of the values of two matrices with different dimensions (1001-by-8501 and 6568-by-1638). Below is the script.
Thanks for your help.

Answers (2)

Do inteprolation using interp2, get them to same size and get the difference. Also have a look on imresize.
The 2nd line of code from the image you shared creates a 3D array of size m x n x 1. The 3d line attempts to concatenate that array along the 3rd dimension. It would have been easier if you included code rather than an image of code so I could copy-paste the lines of code rather than describe them.
The problem is, the value produced by the 3rd line does not have a size of m x n x 1. The error is caused at the 3rd line of code.
If you want to store them in a 3D array, they need to have the same shape and size. One of the matrices is 1001x8501 and the other is 6568x1638. If they are supposed to be the same size, something's wrong with the data or the way the data is being read in. If the sizes are correct, you'll need to explain how you expect the values to pair together.

5 Comments

You forgot to attach your txt files. Come on, make it easy for us to help you, not hard. Please attach them.
Also explain how you'd like to subtract them if they're not the same size. Read Adam's last paragraph again.
Thanks for answer. I give you the link to download the files because they are heavier than 5MB even in zip files ( https://we.tl/t-Hov9V54BYz )
The goal is to calculate the difference between the two DEM and then create a graph (see below).
Thanks for helping.
mnt = zeros(1001,8501,16);
mnt(:,:,1) = dlmread('DEM2707.txt');
mnt(:,:,2) = dlmread('export_mnt_j1_20cm.txt');
%variables
dim = size(mnt);
R = 0.2*ones(dim(1),dim(2)); % resolution: 0,2 m
var_z = 1.96*sqrt(R.^2 +R.^2);
d = 24*60*60; % duree 24h
diffs = zeros(dim(1),dim(2),15);
% erosion et dépots jour par jour
for i= 1:15
diffs(:,:,i) = mnt(:,:,i) - mnt(:,:,i+1);
end
%moyenne journaliere
diff_z = mean(diffs,3);
diff_z(-var_z < diff_z < var_z) = 0;
dlmwrite('diff_z.txt',diff_z)
%%
diff_z = dlmread('diff_z.txt');
dim = size(diff_z)
%image de la dynamique
figure(1)
imagesc(diff_z)
cm = rednblue(100);
%%cm(11,:) = [1,1,1];
%%cm(10,:) = [1,1,1];
colormap(cm);
colorbar
caxis([-0.15 0.15])
axis equal
axis tight
Seeing the code is helpful but we can't move forward until the question in my answer is addressed. See the last paragraph of my answer.
The error happens here
mnt(:,:,2) = dlmread('export_mnt_j1_20cm.txt');
I saw that it's possible to have the same matrix doing a correction with the coordinates. Do you have any advices?
One of the matrices is 1001x8501 and the other is 6568x1638. If they are supposed to be the same size, something's wrong with the data or the way the data is being read in. If the sizes are correct, you'll need to explain how you expect the values to pair together.

Sign in to comment.

Asked:

on 28 Apr 2020

Commented:

on 29 Apr 2020

Community Treasure Hunt

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

Start Hunting!