ismember in a different way

1 Ansicht (letzte 30 Tage)
sprklspring
sprklspring am 20 Jul. 2018
Kommentiert: sprklspring am 20 Jul. 2018
Hello all, I have a vector X with numbers. These X vector numbers are numbers (indeces) for columns of another vector Y that are significant. Now I want to binarize my vector Y and set the values in Y that correspond with the indeces provided by vector X to 1s, and the rest of the vector Y to 0s.
example:
X = [1 2 5 9]; Y = [10 12 3 43 75 60 67 8 90]
result: Y = [1 1 0 0 1 0 0 0 1]
I tried to do it with ismember, but I do not think it will work for indexing - could you help? Thanks.

Akzeptierte Antwort

Jan
Jan am 20 Jul. 2018
Bearbeitet: Jan am 20 Jul. 2018
The values of Y do not matter in any way, do they?
X = [1 2 5 9];
Y = [10 12 3 43 75 60 67 8 90];
Y(:) = 0; % Set all to 0
Y(X) = 1; % Set elements at position X to 1
Or if you really want ismember, but this is less efficient:
Y = ismember(1:numel(Y), X);
  1 Kommentar
sprklspring
sprklspring am 20 Jul. 2018
Thanks Jan, exactly what I was looking for.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by