if condition in for loop
3 views (last 30 days)
Show older comments
Hello ,
I have a problem in finding the same rows between two matrices. I wrote the code as follows
function neigh_no = neigh_search(hex_center,neigh)
[~, n] =size(hex_center);
[~, n1]= size(neigh);
if(n~=n1)
neigh=neigh';
end
neigh_no=[];
for i=1:length(hex_center)
for j=1:length(neigh)
if hex_center(i,:)== neigh(j,:)
neigh_no=[neigh_no i];
end
end
end
end
Suppose if the hex_center array values are
0 0
0 1.7320
0 3.4641
0 5.1961
3 0
3 1.7320
3 3.4641
3 5.1961
6 0
6 1.7320
6 3.4641
6 5.1961
9 0
9 1.7320
9 3.4641
9 5.1961
1.5 0.8660
1.5 2.5980
1.5 4.3301
4.5 0.8660
4.5 2.5980
4.5 4.3301
7.5 0.8660
7.5 2.5980
7.5 4.33012
and the neigh values are
9 5.1961
7.5 6.0621
6 5.1961
6 3.4641
7.5 2.5980
9 3.4641
if you observe that the rows 11,12,15,16 and 24 in hex_center matrix has same rows in the second matrix. But in the output it displays only the rows 11 and 15.Can you please help me? its so important> I know its simple. But I dont know where I did mistake.
0 Comments
Answers (2)
Image Analyst
on 21 Sep 2015
That usually doesn't work for floating point numbers, just integers and inverse powers of 2. You'll have to check for a tolerance like the code examples in the FAQ show you how to do: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
2 Comments
Star Strider
on 21 Sep 2015
Edited: Star Strider
on 21 Sep 2015
Try this one line of code instead of the entire loop structure:
[neigh, neigh_no] = ismember(neigh, hex_center, 'rows');
Also, since ‘neigh_no’ returns one value that is 0, the documentation for ismember explains ‘The output array, Locb, contains 0 wherever A is not a member of B.’ (The Locb variable in the documentation is ‘neigh_no’ here.)
EDIT — Added link to ismember documentation.
3 Comments
Image Analyst
on 22 Sep 2015
Compute the distances. I think you can use pdist() if you have the stats toolbox. Then find distances less than about 1.75 times the separation to find the immediate neighbors.
Star Strider
on 22 Sep 2015
You can introduce a tolerance with the ismember function by using ismembertol (introduced in R2015a).
If you have an earlier version, consider using the newest version of round (with two arguments) to round to a specific number of decimal places. If you don’t have that version of round, you can simulate it with this function to reduce argument precision for ismember:
roundn = @(x,n) round(x*10.^n).*10.^(-n);
It rounds ‘x’ to ‘n’ places to the right of the decimal (n>0) or to the left of the decimal (n<0). For n=0 it’s the usual round function.
Either of these should solve the tolerance problem. (Your data produced the desired result with my original code, so I didn’t initially suggest ismembertol or rounding.)
See Also
Categories
Find more on Labels and Annotations 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!