Element replacement of matrix.

6 Ansichten (letzte 30 Tage)
Usha Silwal
Usha Silwal am 11 Sep. 2019
Beantwortet: David K. am 11 Sep. 2019
How do I replace each element in third row of the 5*4 matrix with its corresponding column number using for loop?

Akzeptierte Antwort

David K.
David K. am 11 Sep. 2019
If using a for loop is not actually a requirement you can do this very simply as such:
myMatrix = rand(5,4); % create 5x4 matrix
myMatrix(3,:) = 1:4; % replace values
If the for loop is a requirement:
myMatrix = rand(5,4); % create 5x4 matrix
for n = 1:4
myMatrix(3,n) = n;
end

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB 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