How to compare pixel co-ordination using a for loop ?

2 Ansichten (letzte 30 Tage)
Piyum Rangana
Piyum Rangana am 24 Mai 2017
Beantwortet: Jan am 26 Mai 2017
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 Kommentare
Piyum Rangana
Piyum Rangana am 24 Mai 2017
Yes it is not equal. How can I check whether a pixel co-ordination of first boundary in a second boundary with this code then ?
Sanket Karnik
Sanket Karnik am 26 Mai 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 26 Mai 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.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by