ismembertol not working correctly

I have two matrices A and B in the attached xls file and run
[~,loc3] = ismembertol(A(:,2),B(:,1),0.01);
and the outcome loc3 also in the xls file. This is not right since loc3 taking values other than 0 and 1. Please advise.

 Accepted Answer

Star Strider
Star Strider on 9 Apr 2018
Only the first output of ismember or ismembertol is a logical array. The second output, the one you are asking for, according to the documentation:
  • LIA = ismembertol(A,B,tol) returns an array containing logical 1 (true) where the elements of A are within tolerance of the elements in B.
  • [LIA,LocB] = ismembertol(A,B,tol) also returns an array, LocB, that contains the index location in B for each element in A that is a member of B.
So the second output is an array of indices (similar to what you would get from the find function), rather than the logical array produced by the first output.

11 Comments

Thank you. But then
A(:,4)=B(loc2,7);
Does not work well. Please advise.
The first output is a logical index into the first argument, and the second output is a numeric index in to the second argument.
For example, one result of this test code:
A = randi(9, 1, 5)
B = randi(9, 1, 8)
[LIA, LOCB] = ismembertol(A,B)
AinB = A(LIA)
BinA = B(LOCB(LOCB>0))
is:
A =
4 7 9 2 1
B =
1 9 1 9 9 6 7 5
LIA =
0 1 1 0 1
LOCB =
0 7 2 0 1
AinB =
7 9 1
BinA =
7 9 1
What would be the correct code?
I am not certain what you want to do.
I would likely not assign the results back into ‘A’. I would assign them to a different vector, perhaps:
B_in_A = B(loc2,7);
or something similar. (Use a variable name that makes sense in the context of your code.) You might also want to test to be certain that ‘loc2’ does not contain any 0 elements, as I did in my example code.
This is what I have been trying to do:
A(:,4)=B(loc2,7);
D=B(loc2,7);
Not working.
Unless ‘B(loc2,7)’ has the same number of elements as ‘A’ (that already seems to exist), that may not work. That is the reason I would output those data to a different vector.
If you want to find the elements in ‘A’ that are present in ‘B’, and output those into a different vector, use the first output of ismembertol as a logical index into ‘A’, for example ‘A(loc1,4)’. (I am guessing here that ‘loc1’ is the first output.)
alpedhuez
alpedhuez on 9 Apr 2018
Edited: alpedhuez on 9 Apr 2018
I want to put, for each element of the 4th column of A,
- first go to the element in the 2nd column of A in the same row
- then look for the element in the 7th column of B where the first column of B of the same row is the same with the 2nd column of A found in the first step
- then substitute the element in the 7th column of B into the 4th column of A
I restarted PC but the results are the same.
I cannot follow what you are doing.
What I have been trying to do is the same with
https://www.mathworks.com/matlabcentral/answers/392053-substitute-values-from-one-matrix-to-another

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products

Tags

Community Treasure Hunt

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

Start Hunting!