# How to subtraction two matrix with different dimensions?

25 views (last 30 days)
Veronika on 11 Apr 2017
Answered: Walter Roberson on 22 Apr 2017
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

the cyclist on 15 Apr 2017
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.
Veronika on 16 Apr 2017
I can´t accept the answer, I didn´t see this option.
the cyclist on 20 Apr 2017
Oh, right. I forgot this all transpired through comments, and not an actual answer. Well, just advice for next time, then.

Walter Roberson on 22 Apr 2017
One approach that calculates the portion that is inside both regions:
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: