How to assign the value of the index to the index position?

2 vues (au cours des 30 derniers jours)
Paul Hinze
Paul Hinze le 6 Déc 2021
Commenté : KSSV le 6 Déc 2021
Hello dear Matlab Community
I have a row vector with the size of 1x40000. The vector consists of zeros and ones.
How can i convert the value ones to the value of their corresponding indices?
Here is a small example
v = [0 0 0 1 1 1 0 0 0]; % This vector ones at positions 4,5, and 6, and I want that the ones get changed by the values 4,5, and 6.
Thank you in advance,
Paul

Réponse acceptée

KSSV
KSSV le 6 Déc 2021
Modifié(e) : KSSV le 6 Déc 2021
v = [0 0 0 1 1 1 0 0 0];
idx = find(v)
idx = 1×3
4 5 6
v(v==1) = find(v)
v = 1×9
0 0 0 4 5 6 0 0 0
  2 commentaires
Paul Hinze
Paul Hinze le 6 Déc 2021
Perfect, thank you
KSSV
KSSV le 6 Déc 2021
Thanks is accepting the answer .. :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by