insert ones at given position of matrix

10 vues (au cours des 30 derniers jours)
Phillip
Phillip le 4 Déc 2017
Commenté : Phillip le 4 Déc 2017
I'm stuck on this probably very simple thing - I have this matrix:
rd = zeros(4, 6);
and x is meant to be the position I would like to insert a 1:
x = [3 1 3 1 1 1];
to obtain this:
0 1 0 1 1 1;
0 0 0 0 0 0;
1 0 1 0 0 0;
0 0 0 0 0 0;
Would anybody please be able to help?

Réponse acceptée

Stephen23
Stephen23 le 4 Déc 2017
Modifié(e) : Stephen23 le 4 Déc 2017
Use sub2ind:
>> rd = zeros(4,6);
>> x = [3,1,3,1,1,1];
>> rd(sub2ind(size(rd),x,1:numel(x))) = 1
rd =
0 1 0 1 1 1
0 0 0 0 0 0
1 0 1 0 0 0
0 0 0 0 0 0

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by