- ism, a logical array the same size as A saying whether each element of A is in B(:,1), i.e., ism is true where the corresponding element of A exists in B(:,1) and ism is false where the corresponding element of A is not in B(:,1)
- idx, the index in B(:,1) where the first instance of each element of A exists, or 0 if that element of A does not exist in B(:,1)
How does ismember and assert work in this code?
조회 수: 6 (최근 30일)
이전 댓글 표시
Previously, I raised a question of how to generate a matrix of only masses; provided that a matrix A is given that contains the object list and a matrix B that contains the objects + their masses. I am new to MATLAB and I want to know how the code works. Thank you!
A = ["C"; "A"; "E"; "F"; "I"]
B = ["A", 1;"B", 34;"C" 56;"D" 32;"E",11;"F",8;"G", 7;"H",9;"I" 77]
[ism,idx] = ismember(A,B(:,1));
assert(all(ism))
result = B(idx,:)
댓글 수: 0
채택된 답변
Voss
2023년 2월 2일
[ism,idx] = ismember(A,B(:,1)); returns
Examples to illustrate:
x = [10;50;70];
y = [10;30;50;70];
[ism,idx] = ismember(x,y)
x = [10;50;70];
y = [10;30;10;30;50;70];
[ism,idx] = ismember(x,y)
x = [10;20;70];
y = [10;30;10;30;50;70];
[ism,idx] = ismember(x,y)
assert is used in this case to make sure that all elements of A exist in B(:,1). If that's not the case, an error is thrown, execution of the code stops, and subsequent commands are not executed.
assert(all(ism))
References:
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!