Specify row and column independently to update matrix?

조회 수: 1 (최근 30일)
Andrew Landau
Andrew Landau 2018년 3월 4일
댓글: Andrew Landau 2018년 3월 4일
Lets say I have a matrix: X = zeros(3);
and want to add a vector: x = [1 2 3]; to the matrix, but add it such that each number is added to a different column in each row defined by y = [1 2 3].
I could write a for loop,
for i = 1:length(x)
X(i,y(i)) = x(i);
end
but it should be possible to do this in a single line....
any suggestions? thanks

채택된 답변

Jos (10584)
Jos (10584) 2018년 3월 4일
doc sub2ind

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by