MATLAB: How to add arrays to specific rows of a matrix in single step indexindexing Suppose I have a matrix A of size (k x n). I want to add an array of size (1 x n) to some of the rows of this matrix, say rows (2,5,7,9). How can this be done in single step ? Best Answer a = randi(9, 10, 4);b = 100*ones(1, 4);rs = [2 5 7 9];a(rs,:) = bsxfun(@plus,a(rs,:),b); Related SolutionsMATLAB: Help running Matlab scriptMATLAB: Is it possible to extract the values with a vector of indices for each row without using the for statement from the matrix see sub2ind()A = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12]; % reference matrixb = [2; 1; 1; 3]; % index for each row that I want to extractidx = sub2ind(size(A), 1:size(A,1), b.');A(idx)Result>> A(idx)ans = 2 4 7 12 Related QuestionHow to creat two box plot in one graph use different dataFind the pair of the matrixHow to modify a matrix to change the entries >=0 and multiply it by 5 AND to change the entries <0 and add 2 to those entriesDivide an array in n different arrays
Best Answer