How to subtraction two matrix with different dimensions?
Show older comments
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
the cyclist
on 11 Apr 2017
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?
the cyclist
on 12 Apr 2017
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;
the cyclist
on 12 Apr 2017
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.
Veronika
on 12 Apr 2017
the cyclist
on 12 Apr 2017
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.
Veronika
on 12 Apr 2017
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
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.
Answers (1)
Walter Roberson
on 22 Apr 2017
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;
Now you can convert both_reg back into polygons. You could use https://www.mathworks.com/matlabcentral/fileexchange/32112-mask2poly or https://www.mathworks.com/matlabcentral/fileexchange/45980-mask2poly-mask- . Or you could bwboundaries or bwtraceboundary
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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!