How to compare pixel co-ordination using a for loop ?
2 views (last 30 days)
Show older comments
Here is my MATLAB code.
for j = 1 : numberOfRegions
thisBoundary = boundaries{j};
x = thisBoundary(:, 1);
y = thisBoundary(:, 2);
for j2 = 1 : numberOfRegions2
thisBoundary2 = boundaries2{j2};
x2 = thisBoundary2(:, 1);
y2 = thisBoundary2(:, 2);
if(x2==x)
if (y2 == y)
U = x2;
V = y2;
end
end
end
end
In this code I tried to compare boundary pixel co-ordinations(x,y) of two masks which I already obtained. But when it comes to the if condition it gives following error. I want to obtain the matched pixel co-ordinations and store them separately. how can I fixed that?
_ Matrix dimensions must agree. Error in (line 130) if(x2==x)_
3 Comments
Sanket Karnik
on 26 May 2017
I understand that you are getting "Matrix dimensions must agree" error when you try to compare 2 matrices. As KSSV mentioned this error message suggests that you are trying to perform matrix operations on matrices of unequal dimension. If you are comparing two matrices then it is very important that both matrices are of same size. In order to answer your next question, please post here the data you are using so that we can try to advice you by referring to that data.
Accepted Answer
Jan
on 26 May 2017
You have to define, what should happen in x2==x. What do you want to compare? x2==x is an elementwise comparison: Either both arrays need the same size or one is a scalar. Internally the IF-condition is converted to a scalar, as needed:
if all(reshape(x2==x, 1, [])) && ~isempty(x2==x)
This must still fail. But it demonstrates, that there is more to define than just using the == operator. Perhaps you want:
if any(ismember(x2, x))
or
if all(ismember(x2, x))
or
if any(ismember(x, x2))
or what ever. You have to explain this, before a specific solution can be suggested.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!