简体   繁体   English

为矩阵的每一行设置特定列的值

[英]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.列索引因行而异,并由列向量am值)指定。 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.

相关问题 根据矩阵中列的值查找特定行 - Find a Specific row based on value of a column in a matrix MATLAB:如何生成每行和每列具有特定数量 1 的随机二进制矩阵? - MATLAB : How can I generate a random binary matrix with a specific number of 1s in each row and in each column? 保存在每个循环中创建的矩阵值保存在公共矩阵中,每个循环矩阵具有相同的行大小,但列数 - save matrix value which create in each loop save in common matrix ,each loop matrix has same row size but column is ow 矩阵各列的平均值 - Mean value of each column of a matrix 构造一个二进制矩阵,使得每一列仅包含单个“ 1”,并且每一行的总和具有期望的值 - construct a binary matrix such that each column contains only single “1” and sum of each row is of desired value 在矩阵的每一行中查找元素的列位置,并存储在矩阵中 - Find the column location of an element each each row of a matrix, and storing in matrix 如何在Matlab中设置数组中每一行(或每一列)的最小值? - How to set in Matlab the min value of each row (or column) in array? 对双精度矩阵中每一行的特定列求和 - Summing specific columns for each row in a matrix of double 矩阵的特定列中的最大值 - Maximum value in a specific column of a matrix 在矩阵的每一行中找到1的列索引 - Finding the column index for the 1 in each row of a matrix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM