[英]Set a value of a specific column for each row of a matrix
I have a matrix A
with m
rows and I'd like to set a specific element of each row equal 1. The column index varies from row to row and is specified by a column vector a
(with m
values). 我有一个m
行的矩阵A
,我想将每行的特定元素设置为1.列索引因行而异,并由列向量a
( m
值)指定。 That is, I want A_{i,a_i} = 1
. 也就是说,我想要A_{i,a_i} = 1
。 Is there a quick way to do this in Matlab (without a for-loop)? 在Matlab中有没有快速的方法(没有for循环)?
我使用sub2ind
函数解决了它:
A(sub2ind(size(A), 1:numel(a), a')) = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.