Effacer les filtres
Effacer les filtres

What wrong with the indexing code?

2 vues (au cours des 30 derniers jours)
GEORGIOS BEKAS
GEORGIOS BEKAS le 8 Fév 2018
Commenté : GEORGIOS BEKAS le 8 Fév 2018
If I want to fill in a matrix, of which I have its indices without a for loop, how could I do it? Suppose that the (row(i),col(i)) elements to be filled are given.
value = 189;
row = [ 1 3 6 7 8 ];
col = [ 1 1 4 4 5];
B(row,col) = value

Réponse acceptée

Walter Roberson
Walter Roberson le 8 Fév 2018
B = full( sparse(row, col, value) );
or
B = accumarray([row(:), col(:)], value);
  2 commentaires
GEORGIOS BEKAS
GEORGIOS BEKAS le 8 Fév 2018
how about using arrayfun/bsxfun or something?
Walter Roberson
Walter Roberson le 8 Fév 2018
Not at all easily with arrayfun or bsxfun as assignment through function calls is difficult and never less than ugly.

Connectez-vous pour commenter.

Plus de réponses (1)

Birdman
Birdman le 8 Fév 2018
B(sub2ind([max(row) max(col)],row,col)) = value
  8 commentaires
Birdman
Birdman le 8 Fév 2018
Doesn't my answer also work Walter?
GEORGIOS BEKAS
GEORGIOS BEKAS le 8 Fév 2018
it did not :/

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by