MATLAB Answers

Element replacement of matrix.

1 view (last 30 days)
Usha Silwal
Usha Silwal on 11 Sep 2019
Answered: David K. on 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?

  0 Comments

Sign in to comment.

Accepted Answer

David K.
David K. on 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

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.