How to subtraction two matrix with different dimensions?

Dear all,
I have two matrix. thisBoundary1=1232x2double and thisBoundary2=1237x2double. And I would like to subtraction these two like :
dech = thisBoundary2 - thisBoundary1
But I have this error:
Error using -
Matrix dimensions must agree.
Error in dechova_krivka (line 74)
dech = thisBoundary2 - thisBoundary1
Can you please help me, how to modify these two matrix for subtraction?

9 Comments

Let's simplify this a little. Suppose I tell you that I want to subtract v2 from v1, where
v1 = [2 4 3]
v2 = [5 1]
What is the answer to that?
Moved this comment from Asaduzzaman Md here (rather than being an answer), since it was a response to my comment.
===========================================
You can add some zeros or delete some extra element for making the same size. v1 = [2 4 3] v2 = [5 1]
Deletion process v1=v1(1:2); ans=v2-v1;
or adding zeros v2=[v2 0]; ans=v2-v1;
We could also sort the vectors, interpolate, and subtract. Or we could randomly select the middle element to remove, and then subtract.
My point is that we don't have enough information to know what the correct approach is, because the poster has not explained enough.
Okay, I tried adding zeros, like you said, but this error appears:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in dechova_krivka (line 74)
thisBoundary2=[thisBoundary2 0]
So I attach this two data thisBoundary1 and thisBoundary2 for better explaination.
Thank you for your time and answer.
There is some very fundamental misunderstanding of both MATLAB and math going on here.
If your goal is to make the two arrays the same size, then
thisBoundary1_bigger = [thisBoundary1; zeros(5,2)];
will match the size of thisBoundary2, and then you will be able to subtract the two arrays.
But how do you know that you are subtracting the correct elements from each other, and getting a sensible result? You could also have done
thisBoundary1_bigger = [zeros(5,2); thisBoundary1];
which adds the zeros at the top.
This will give different results when subtracted. You need to think about what you are trying to calculate, not just blindly append zeros and plow through.
Thank you for your answer, it helps.
The best form of thanks is to accept and/or upvote helpful answers. This rewards the contributor, and can guide future users to the most useful answers.
I can´t accept the answer, I didn´t see this option.
Oh, right. I forgot this all transpired through comments, and not an actual answer. Well, just advice for next time, then.

Sign in to comment.

Answers (1)

One approach that calculates the portion that is inside both regions:
load thisBoundary1
load thisBoundary2
maxdim = max([thisBoundary1(:); thisBoundary2(:)]);
reg1 = poly2mask( thisBoundary1(:,1), thisBoundary1(:,2), maxdim, maxdim );
reg2 = poly2mask( thisBoundary2(:,1), thisBoundary2(:,2), maxdim, maxdim );
both_reg = reg1 & reg2;
A different approach is to use https://www.mathworks.com/matlabcentral/fileexchange/22441-curve-intersections to find the intersections of the regions and then do something with that intersection information. You would use this in the situation where you needed to calculate the intersections as exactly as feasible. Your coordinates appear to all be integers, so I doubt you need this.
A third approach that calculates the area that is inside either region is:
load thisBoundary1
load thisBoundary2
x = [thisBoundary1(:,1); thisBoundary2(:,1)];
y = [thisBoundary1(:,2); thisBoundary2(:,2)];
k = boundary(x, y);
plot(x(k), y(k))
A fourth approach would be to use image registration techniques to align the two for best match before doing one of the techniques described above.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Asked:

on 11 Apr 2017

Answered:

on 22 Apr 2017

Community Treasure Hunt

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

Start Hunting!