MATLAB Answers


A basic question of matrix indexing can't get a proper output

Asked by Debaditya Chakraborty on 20 May 2019
Latest activity Edited by Adam Danz
on 21 May 2019
Given matrix A, assign the second column of A to a variable v. Afterwards change each element of the last row of A to 0.
my code:
A = [1:5; 6:10; 11:15; 16:20];
v= A(1:4,2);
A(5, :) = zeros(1, 5);


1 Answer

Answer by Adam Danz
on 20 May 2019
Edited by Adam Danz
on 21 May 2019

Here are some improvements to your code so that it works no matter what size A is.
A = [1:5; 6:10; 11:15; 16:20];
v= A(:,2);
A(end+1, :) = zeros(1, size(A,2));
Note that your instructions are to "change" the last row of A. That's not what your code is doing. You're adding a row of zeros. If you want the change the last row instead of adding another row,
A(end, :) = zeros(1, size(A,2));


Sign in to comment.