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)?
Asked
Active
Viewed 318 times
1

John Manak
- 13,328
- 29
- 78
- 119
1 Answers
3
I solved it using the sub2ind
function:
A(sub2ind(size(A), 1:numel(a), a')) = 1

John Manak
- 13,328
- 29
- 78
- 119
-
That is indeed the way to do it – Diederick C. Niehorster Jan 07 '14 at 05:36