Use of Matlab find function for getting index of a vertex using its coordinates
I have a matrix 50943x3 that contains vertices of a surface mesh.I want to find the index of a certain vertex using its coordinates (x,y,z).
I tried the Matlab function find but it return an empty matrix 0-by-1.
Thanks in advance,
Cheers
matlab find mesh surface vertex
add a comment |
I have a matrix 50943x3 that contains vertices of a surface mesh.I want to find the index of a certain vertex using its coordinates (x,y,z).
I tried the Matlab function find but it return an empty matrix 0-by-1.
Thanks in advance,
Cheers
matlab find mesh surface vertex
2
You should post your code to show what has been attempted
– Brice
Nov 19 '18 at 14:39
add a comment |
I have a matrix 50943x3 that contains vertices of a surface mesh.I want to find the index of a certain vertex using its coordinates (x,y,z).
I tried the Matlab function find but it return an empty matrix 0-by-1.
Thanks in advance,
Cheers
matlab find mesh surface vertex
I have a matrix 50943x3 that contains vertices of a surface mesh.I want to find the index of a certain vertex using its coordinates (x,y,z).
I tried the Matlab function find but it return an empty matrix 0-by-1.
Thanks in advance,
Cheers
matlab find mesh surface vertex
matlab find mesh surface vertex
asked Nov 19 '18 at 14:25


amine
73
73
2
You should post your code to show what has been attempted
– Brice
Nov 19 '18 at 14:39
add a comment |
2
You should post your code to show what has been attempted
– Brice
Nov 19 '18 at 14:39
2
2
You should post your code to show what has been attempted
– Brice
Nov 19 '18 at 14:39
You should post your code to show what has been attempted
– Brice
Nov 19 '18 at 14:39
add a comment |
2 Answers
2
active
oldest
votes
Your attempt probably does not work because of floating point rounding errors. You can read more about it here. You could look into the the eps function, or just use this example:
% Your matrix
M = randn(50943 , 3);
% The coordinates you are looking for
P = [0,0,0];
% Distance between all coordinates and target point
D = sqrt(sum((M - bsxfun(@minus,M,P)).^2,2));
% Closest coordinates to target
[~ , pos] = min(D);
% Display result
disp(M(pos , :))
pos = find(D<tol);
wheretol
is a suitable tolerance would work too, in place of[~ , pos] = min(D);
. It will sucessfully return 2 indices if there are two matching points, and and empty array if there is none, whilemin
will always return one index. It depends whether these cases may be expected or not and what should be done in that case.
– Brice
Nov 19 '18 at 14:37
I used exactly this example to understand it but it returns a Matrix dimensions must agree error at the lineD = sqrt(sum((M - P).^2,2));
– amine
Nov 19 '18 at 14:47
@amine Probably a question of Matlab version; I corrected the example to make it compatible with older versions which do not automatically expand matrices.
– Zep
Nov 19 '18 at 14:54
@Brice, OP wants to find "find the index of a certain vertex" from its coordinates, not all the vertexes close to a given point. I think the solution should only return one point! Otherwise, yes, your modification would work.
– Zep
Nov 19 '18 at 14:55
@Zep, to be picky, to get compatibility with pre-R2016b versions of Matlab, it is adviseable to replace(M-P)
withbsxfun(@minus,M,P)
rather than using arepmat
. Performance is better and the code shorter. And it is backward-compatible down to version R2007a.
– Brice
Nov 19 '18 at 15:05
|
show 1 more comment
Try the following:
mat = randi(30,50943,3);
vec = [1,2,3];
% R2106b+ code
ind = find(all(mat==vec,2));
% or: explicit expansion, works with all versions
ind = find(all(bsxfun(@eq,mat,vec),2));
What it does:
==
or eq
will check if coordinates are equal (gives a [50943x3] bool matrix)
all
will return true only if all coordinates are equal
find
returns the index of all non zero elements
This works only for an exact match (hence the integer coordinates picked with randi
).
Since the answer is already accepted, I'll add @Zep answer that provide a solution to get the nearest point, which seem to be what was initially sought.
[min_dist,ind_nearest] = min(sum(bsxfun(@minus,mat,vec).^2,2)); % index to the nearest point
Thank you very much, it works. Now, ifvec
does not exist in themat
matrix, how to get the nearest vertex ?
– amine
Nov 19 '18 at 14:57
If you want the nearest, then use the answer from @Zep, computing the distance and looking for the minimum. This proposal will only work with an exact match (hence the example withrandi
and integer coordinates)
– Brice
Nov 19 '18 at 15:05
Yep it works, thank you for the two suggestions, I will keep it in mind
– amine
Nov 19 '18 at 15:09
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376703%2fuse-of-matlab-find-function-for-getting-index-of-a-vertex-using-its-coordinates%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your attempt probably does not work because of floating point rounding errors. You can read more about it here. You could look into the the eps function, or just use this example:
% Your matrix
M = randn(50943 , 3);
% The coordinates you are looking for
P = [0,0,0];
% Distance between all coordinates and target point
D = sqrt(sum((M - bsxfun(@minus,M,P)).^2,2));
% Closest coordinates to target
[~ , pos] = min(D);
% Display result
disp(M(pos , :))
pos = find(D<tol);
wheretol
is a suitable tolerance would work too, in place of[~ , pos] = min(D);
. It will sucessfully return 2 indices if there are two matching points, and and empty array if there is none, whilemin
will always return one index. It depends whether these cases may be expected or not and what should be done in that case.
– Brice
Nov 19 '18 at 14:37
I used exactly this example to understand it but it returns a Matrix dimensions must agree error at the lineD = sqrt(sum((M - P).^2,2));
– amine
Nov 19 '18 at 14:47
@amine Probably a question of Matlab version; I corrected the example to make it compatible with older versions which do not automatically expand matrices.
– Zep
Nov 19 '18 at 14:54
@Brice, OP wants to find "find the index of a certain vertex" from its coordinates, not all the vertexes close to a given point. I think the solution should only return one point! Otherwise, yes, your modification would work.
– Zep
Nov 19 '18 at 14:55
@Zep, to be picky, to get compatibility with pre-R2016b versions of Matlab, it is adviseable to replace(M-P)
withbsxfun(@minus,M,P)
rather than using arepmat
. Performance is better and the code shorter. And it is backward-compatible down to version R2007a.
– Brice
Nov 19 '18 at 15:05
|
show 1 more comment
Your attempt probably does not work because of floating point rounding errors. You can read more about it here. You could look into the the eps function, or just use this example:
% Your matrix
M = randn(50943 , 3);
% The coordinates you are looking for
P = [0,0,0];
% Distance between all coordinates and target point
D = sqrt(sum((M - bsxfun(@minus,M,P)).^2,2));
% Closest coordinates to target
[~ , pos] = min(D);
% Display result
disp(M(pos , :))
pos = find(D<tol);
wheretol
is a suitable tolerance would work too, in place of[~ , pos] = min(D);
. It will sucessfully return 2 indices if there are two matching points, and and empty array if there is none, whilemin
will always return one index. It depends whether these cases may be expected or not and what should be done in that case.
– Brice
Nov 19 '18 at 14:37
I used exactly this example to understand it but it returns a Matrix dimensions must agree error at the lineD = sqrt(sum((M - P).^2,2));
– amine
Nov 19 '18 at 14:47
@amine Probably a question of Matlab version; I corrected the example to make it compatible with older versions which do not automatically expand matrices.
– Zep
Nov 19 '18 at 14:54
@Brice, OP wants to find "find the index of a certain vertex" from its coordinates, not all the vertexes close to a given point. I think the solution should only return one point! Otherwise, yes, your modification would work.
– Zep
Nov 19 '18 at 14:55
@Zep, to be picky, to get compatibility with pre-R2016b versions of Matlab, it is adviseable to replace(M-P)
withbsxfun(@minus,M,P)
rather than using arepmat
. Performance is better and the code shorter. And it is backward-compatible down to version R2007a.
– Brice
Nov 19 '18 at 15:05
|
show 1 more comment
Your attempt probably does not work because of floating point rounding errors. You can read more about it here. You could look into the the eps function, or just use this example:
% Your matrix
M = randn(50943 , 3);
% The coordinates you are looking for
P = [0,0,0];
% Distance between all coordinates and target point
D = sqrt(sum((M - bsxfun(@minus,M,P)).^2,2));
% Closest coordinates to target
[~ , pos] = min(D);
% Display result
disp(M(pos , :))
Your attempt probably does not work because of floating point rounding errors. You can read more about it here. You could look into the the eps function, or just use this example:
% Your matrix
M = randn(50943 , 3);
% The coordinates you are looking for
P = [0,0,0];
% Distance between all coordinates and target point
D = sqrt(sum((M - bsxfun(@minus,M,P)).^2,2));
% Closest coordinates to target
[~ , pos] = min(D);
% Display result
disp(M(pos , :))
edited Nov 19 '18 at 15:10
answered Nov 19 '18 at 14:32
Zep
1,203313
1,203313
pos = find(D<tol);
wheretol
is a suitable tolerance would work too, in place of[~ , pos] = min(D);
. It will sucessfully return 2 indices if there are two matching points, and and empty array if there is none, whilemin
will always return one index. It depends whether these cases may be expected or not and what should be done in that case.
– Brice
Nov 19 '18 at 14:37
I used exactly this example to understand it but it returns a Matrix dimensions must agree error at the lineD = sqrt(sum((M - P).^2,2));
– amine
Nov 19 '18 at 14:47
@amine Probably a question of Matlab version; I corrected the example to make it compatible with older versions which do not automatically expand matrices.
– Zep
Nov 19 '18 at 14:54
@Brice, OP wants to find "find the index of a certain vertex" from its coordinates, not all the vertexes close to a given point. I think the solution should only return one point! Otherwise, yes, your modification would work.
– Zep
Nov 19 '18 at 14:55
@Zep, to be picky, to get compatibility with pre-R2016b versions of Matlab, it is adviseable to replace(M-P)
withbsxfun(@minus,M,P)
rather than using arepmat
. Performance is better and the code shorter. And it is backward-compatible down to version R2007a.
– Brice
Nov 19 '18 at 15:05
|
show 1 more comment
pos = find(D<tol);
wheretol
is a suitable tolerance would work too, in place of[~ , pos] = min(D);
. It will sucessfully return 2 indices if there are two matching points, and and empty array if there is none, whilemin
will always return one index. It depends whether these cases may be expected or not and what should be done in that case.
– Brice
Nov 19 '18 at 14:37
I used exactly this example to understand it but it returns a Matrix dimensions must agree error at the lineD = sqrt(sum((M - P).^2,2));
– amine
Nov 19 '18 at 14:47
@amine Probably a question of Matlab version; I corrected the example to make it compatible with older versions which do not automatically expand matrices.
– Zep
Nov 19 '18 at 14:54
@Brice, OP wants to find "find the index of a certain vertex" from its coordinates, not all the vertexes close to a given point. I think the solution should only return one point! Otherwise, yes, your modification would work.
– Zep
Nov 19 '18 at 14:55
@Zep, to be picky, to get compatibility with pre-R2016b versions of Matlab, it is adviseable to replace(M-P)
withbsxfun(@minus,M,P)
rather than using arepmat
. Performance is better and the code shorter. And it is backward-compatible down to version R2007a.
– Brice
Nov 19 '18 at 15:05
pos = find(D<tol);
where tol
is a suitable tolerance would work too, in place of [~ , pos] = min(D);
. It will sucessfully return 2 indices if there are two matching points, and and empty array if there is none, while min
will always return one index. It depends whether these cases may be expected or not and what should be done in that case.– Brice
Nov 19 '18 at 14:37
pos = find(D<tol);
where tol
is a suitable tolerance would work too, in place of [~ , pos] = min(D);
. It will sucessfully return 2 indices if there are two matching points, and and empty array if there is none, while min
will always return one index. It depends whether these cases may be expected or not and what should be done in that case.– Brice
Nov 19 '18 at 14:37
I used exactly this example to understand it but it returns a Matrix dimensions must agree error at the line
D = sqrt(sum((M - P).^2,2));
– amine
Nov 19 '18 at 14:47
I used exactly this example to understand it but it returns a Matrix dimensions must agree error at the line
D = sqrt(sum((M - P).^2,2));
– amine
Nov 19 '18 at 14:47
@amine Probably a question of Matlab version; I corrected the example to make it compatible with older versions which do not automatically expand matrices.
– Zep
Nov 19 '18 at 14:54
@amine Probably a question of Matlab version; I corrected the example to make it compatible with older versions which do not automatically expand matrices.
– Zep
Nov 19 '18 at 14:54
@Brice, OP wants to find "find the index of a certain vertex" from its coordinates, not all the vertexes close to a given point. I think the solution should only return one point! Otherwise, yes, your modification would work.
– Zep
Nov 19 '18 at 14:55
@Brice, OP wants to find "find the index of a certain vertex" from its coordinates, not all the vertexes close to a given point. I think the solution should only return one point! Otherwise, yes, your modification would work.
– Zep
Nov 19 '18 at 14:55
@Zep, to be picky, to get compatibility with pre-R2016b versions of Matlab, it is adviseable to replace
(M-P)
with bsxfun(@minus,M,P)
rather than using a repmat
. Performance is better and the code shorter. And it is backward-compatible down to version R2007a.– Brice
Nov 19 '18 at 15:05
@Zep, to be picky, to get compatibility with pre-R2016b versions of Matlab, it is adviseable to replace
(M-P)
with bsxfun(@minus,M,P)
rather than using a repmat
. Performance is better and the code shorter. And it is backward-compatible down to version R2007a.– Brice
Nov 19 '18 at 15:05
|
show 1 more comment
Try the following:
mat = randi(30,50943,3);
vec = [1,2,3];
% R2106b+ code
ind = find(all(mat==vec,2));
% or: explicit expansion, works with all versions
ind = find(all(bsxfun(@eq,mat,vec),2));
What it does:
==
or eq
will check if coordinates are equal (gives a [50943x3] bool matrix)
all
will return true only if all coordinates are equal
find
returns the index of all non zero elements
This works only for an exact match (hence the integer coordinates picked with randi
).
Since the answer is already accepted, I'll add @Zep answer that provide a solution to get the nearest point, which seem to be what was initially sought.
[min_dist,ind_nearest] = min(sum(bsxfun(@minus,mat,vec).^2,2)); % index to the nearest point
Thank you very much, it works. Now, ifvec
does not exist in themat
matrix, how to get the nearest vertex ?
– amine
Nov 19 '18 at 14:57
If you want the nearest, then use the answer from @Zep, computing the distance and looking for the minimum. This proposal will only work with an exact match (hence the example withrandi
and integer coordinates)
– Brice
Nov 19 '18 at 15:05
Yep it works, thank you for the two suggestions, I will keep it in mind
– amine
Nov 19 '18 at 15:09
add a comment |
Try the following:
mat = randi(30,50943,3);
vec = [1,2,3];
% R2106b+ code
ind = find(all(mat==vec,2));
% or: explicit expansion, works with all versions
ind = find(all(bsxfun(@eq,mat,vec),2));
What it does:
==
or eq
will check if coordinates are equal (gives a [50943x3] bool matrix)
all
will return true only if all coordinates are equal
find
returns the index of all non zero elements
This works only for an exact match (hence the integer coordinates picked with randi
).
Since the answer is already accepted, I'll add @Zep answer that provide a solution to get the nearest point, which seem to be what was initially sought.
[min_dist,ind_nearest] = min(sum(bsxfun(@minus,mat,vec).^2,2)); % index to the nearest point
Thank you very much, it works. Now, ifvec
does not exist in themat
matrix, how to get the nearest vertex ?
– amine
Nov 19 '18 at 14:57
If you want the nearest, then use the answer from @Zep, computing the distance and looking for the minimum. This proposal will only work with an exact match (hence the example withrandi
and integer coordinates)
– Brice
Nov 19 '18 at 15:05
Yep it works, thank you for the two suggestions, I will keep it in mind
– amine
Nov 19 '18 at 15:09
add a comment |
Try the following:
mat = randi(30,50943,3);
vec = [1,2,3];
% R2106b+ code
ind = find(all(mat==vec,2));
% or: explicit expansion, works with all versions
ind = find(all(bsxfun(@eq,mat,vec),2));
What it does:
==
or eq
will check if coordinates are equal (gives a [50943x3] bool matrix)
all
will return true only if all coordinates are equal
find
returns the index of all non zero elements
This works only for an exact match (hence the integer coordinates picked with randi
).
Since the answer is already accepted, I'll add @Zep answer that provide a solution to get the nearest point, which seem to be what was initially sought.
[min_dist,ind_nearest] = min(sum(bsxfun(@minus,mat,vec).^2,2)); % index to the nearest point
Try the following:
mat = randi(30,50943,3);
vec = [1,2,3];
% R2106b+ code
ind = find(all(mat==vec,2));
% or: explicit expansion, works with all versions
ind = find(all(bsxfun(@eq,mat,vec),2));
What it does:
==
or eq
will check if coordinates are equal (gives a [50943x3] bool matrix)
all
will return true only if all coordinates are equal
find
returns the index of all non zero elements
This works only for an exact match (hence the integer coordinates picked with randi
).
Since the answer is already accepted, I'll add @Zep answer that provide a solution to get the nearest point, which seem to be what was initially sought.
[min_dist,ind_nearest] = min(sum(bsxfun(@minus,mat,vec).^2,2)); % index to the nearest point
edited Nov 19 '18 at 15:10
answered Nov 19 '18 at 14:32
Brice
1,359110
1,359110
Thank you very much, it works. Now, ifvec
does not exist in themat
matrix, how to get the nearest vertex ?
– amine
Nov 19 '18 at 14:57
If you want the nearest, then use the answer from @Zep, computing the distance and looking for the minimum. This proposal will only work with an exact match (hence the example withrandi
and integer coordinates)
– Brice
Nov 19 '18 at 15:05
Yep it works, thank you for the two suggestions, I will keep it in mind
– amine
Nov 19 '18 at 15:09
add a comment |
Thank you very much, it works. Now, ifvec
does not exist in themat
matrix, how to get the nearest vertex ?
– amine
Nov 19 '18 at 14:57
If you want the nearest, then use the answer from @Zep, computing the distance and looking for the minimum. This proposal will only work with an exact match (hence the example withrandi
and integer coordinates)
– Brice
Nov 19 '18 at 15:05
Yep it works, thank you for the two suggestions, I will keep it in mind
– amine
Nov 19 '18 at 15:09
Thank you very much, it works. Now, if
vec
does not exist in the mat
matrix, how to get the nearest vertex ?– amine
Nov 19 '18 at 14:57
Thank you very much, it works. Now, if
vec
does not exist in the mat
matrix, how to get the nearest vertex ?– amine
Nov 19 '18 at 14:57
If you want the nearest, then use the answer from @Zep, computing the distance and looking for the minimum. This proposal will only work with an exact match (hence the example with
randi
and integer coordinates)– Brice
Nov 19 '18 at 15:05
If you want the nearest, then use the answer from @Zep, computing the distance and looking for the minimum. This proposal will only work with an exact match (hence the example with
randi
and integer coordinates)– Brice
Nov 19 '18 at 15:05
Yep it works, thank you for the two suggestions, I will keep it in mind
– amine
Nov 19 '18 at 15:09
Yep it works, thank you for the two suggestions, I will keep it in mind
– amine
Nov 19 '18 at 15:09
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376703%2fuse-of-matlab-find-function-for-getting-index-of-a-vertex-using-its-coordinates%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You should post your code to show what has been attempted
– Brice
Nov 19 '18 at 14:39