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
the cyclist
2017년 4월 11일
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
2017년 4월 12일
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
2017년 4월 12일
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
2017년 4월 12일
the cyclist
2017년 4월 12일
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
2017년 4월 12일
the cyclist
2017년 4월 15일
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
2017년 4월 16일
the cyclist
2017년 4월 20일
Oh, right. I forgot this all transpired through comments, and not an actual answer. Well, just advice for next time, then.
답변 (1개)
Walter Roberson
2017년 4월 22일
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.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!