finding elements in a vector from another vector

조회 수: 158 (최근 30일)
Paul Rogers
Paul Rogers 2020년 7월 18일
댓글: Star Strider 2020년 7월 18일
I'd like to create a vector y with the positions of the elements of x in m.
i.e.
x=0 (first element of x) has and index of 1 in m. So y(1)=1
x=241 (second element of x) has index of 242 in x. So y(2)=242
m and x are in attached

채택된 답변

Star Strider
Star Strider 2020년 7월 18일
Try this:
M = load('m.mat');
X = load('x.mat');
m = M.m;
x = X.x;
[~,y] = ismembertol(x, m, 1E-4)
producing:
y =
1
242
937
2001
3306
4695
6001
7065
7759
8001
I checked that separately for a few values using find, and it appears to produce the correct result.
.
  댓글 수: 8
Paul Rogers
Paul Rogers 2020년 7월 18일
I think it's best we don't edit comment so people can read different options and figure it out based on their version
Star Strider
Star Strider 2020년 7월 18일
Noted.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2020년 7월 18일
편집: Fangjun Jiang 2020년 7월 18일
[~,y]=ismember(x,m)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by