What wrong with the indexing code?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2018-2-8
B = full( sparse(row, col, value) );
or
B = accumarray([row(:), col(:)], value);
2 个评论
Walter Roberson
2018-2-8
Not at all easily with arrayfun or bsxfun as assignment through function calls is difficult and never less than ugly.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!