Specify row and column independently to update matrix?

1 Ansicht (letzte 30 Tage)
Andrew Landau
Andrew Landau am 4 Mär. 2018
Kommentiert: Andrew Landau am 4 Mär. 2018
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

Akzeptierte Antwort

Jos (10584)
Jos (10584) am 4 Mär. 2018

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by