为矩阵的每一行设置特定列的值 [英] Set a value of a specific column for each row of a matrix
本文介绍了为矩阵的每一行设置特定列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含m
行的矩阵A
,我想将每一行的特定元素设置为1.列索引因行而异,并由列向量a
指定(与m
值).也就是说,我要A_{i,a_i} = 1
.在Matlab中有没有一种快速的方法(没有for循环)?
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). That is, I want A_{i,a_i} = 1
. Is there a quick way to do this in Matlab (without a for-loop)?
推荐答案
I solved it using the sub2ind
function:
A(sub2ind(size(A), 1:numel(a), a')) = 1
这篇关于为矩阵的每一行设置特定列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文