MATLAB Answers

finding elements in a vector from another vector

8 views (last 30 days)
Paul Rogers
Paul Rogers on 18 Jul 2020
Commented: Star Strider on 18 Jul 2020
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

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 18 Jul 2020
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 Comments

Show 5 older comments
Star Strider
Star Strider on 18 Jul 2020
Great!
I did not initially pick up on your posting the Release information as R2014b (so my apologies for that oversight if you posted it originally, since I only checked later).
.
Paul Rogers
Paul Rogers on 18 Jul 2020
I think it's best we don't edit comment so people can read different options and figure it out based on their version

Sign in to comment.

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 18 Jul 2020
Edited: Fangjun Jiang on 18 Jul 2020
[~,y]=ismember(x,m)

  0 Comments

Sign in to comment.

Products


Release

R2014b

Community Treasure Hunt

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

Start Hunting!