S=[1 3 0 2 0 0];
C=[0 2];
[~,col] = ismember(C,S);
S(col) = [];
S =
1 3 0 0
This code is for : exclude C from S with repetition while keeping the same order of elements in S.
But how it works?
[~,col] = ismember(C,S);

 채택된 답변

Guillaume
Guillaume 2018년 10월 23일
편집: Guillaume 2018년 10월 23일

1 개 추천

I'm not sure what there is to explain. As documented, col tells you where each element corresponding element of C is found in S. If the element of C is found in more than one position, you'll get the first one of these.
The code is also flawed and will error if any element of C is not found at all in S. The safe version of the code would be:
S = [1 3 0 2 0 0];
C = [0 2 4]; %note that the original code would error because 4 is not present is S
[found, where] = ismember(C, S);
S(where(found)) = [] %this will not error

댓글 수: 4

IBM watson
IBM watson 2018년 10월 23일
I am just confused about "~". What does this stand for?
Oh! the ~ in a function output just means ignore that output as the author was only interested in the 2nd output.
However, as I've shown you can't ignore this output in case an element of C is not in S.
You'll find that ~ is often used when calling min or max when you're just interested in knowing where the min or max is, but not its value:
value = max(somearray); %want to know what the max is
[value, where] = max(somearray); %wamt to know what the max is and where it is
[~, where] = max(somearray); %don't care about the value of the max, just want to know where it is
IBM watson
IBM watson 2018년 10월 23일
Many thanks to you!
Note that the code from above assumes that the elements in C are distinct.
So setting
S=[1 3 0 2 0 0];
C=[0 0 2];
will not produce
S=[1 3 0]
but also
S=[1 3 0 0]
Best wishes
Torsten.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2018년 10월 23일

댓글:

2018년 10월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by