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.
采纳的回答
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 个评论
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.)
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
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Import and Analysis 的更多信息
产品
标签
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
