What wrong with the indexing code?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
GEORGIOS BEKAS
il 8 Feb 2018
Commentato: GEORGIOS BEKAS
il 8 Feb 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
0 Commenti
Risposta accettata
Walter Roberson
il 8 Feb 2018
B = full( sparse(row, col, value) );
or
B = accumarray([row(:), col(:)], value);
2 Commenti
Walter Roberson
il 8 Feb 2018
Not at all easily with arrayfun or bsxfun as assignment through function calls is difficult and never less than ugly.
Più risposte (1)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!