A basic question of matrix indexing can't get a proper output
262 views (last 30 days)
Show older comments
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);
20 Comments
Walter Roberson
on 11 Jan 2023 at 6:24
Assigning to A(5,:) would be appropriate only for the cases where A happens to already have exactly 5 rows. The example A matrix has 4 rows, not 5. Assigning to A(5,:) for it would create a new row of zeros, whereas the requirements is that the last (existing) row of A is set to 0.
Answers (9)
amjad khan
on 3 Apr 2020
A = [1:5; 6:10; 11:15; 16:20];
v=A(:,2)
A(4,:)=0
2 Comments
Adam Danz
on 3 Apr 2020
This is essentially the same answer as mine except that you're replacing the 4th row with 0s whether or not the 4th row is the last row. This is why I suggest using A(end,:) = 0 so that it will work for all sizes of A.
Adam Danz
on 20 May 2019
Edited: Adam Danz
on 16 May 2020
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 to change the last row instead of adding another row,
A(end, :) = zeros(1, size(A,2));
Summary
Add a row of 0s to the end of matrix A
A(end+1,:) = 0;
Replace the last row of matrix A with 0s.
A(end,:) = 0;
0 Comments
John Cyruz Las Piñas
on 10 Mar 2020
A = [1:5; 6:10; 11:15; 16:20];
v= A(:,2);
A(end+1, :) = zeros(1, size(A,2));
4 Comments
Badal Bhardwaj
on 12 May 2020
Answer is A(4,1)=0 A(4,2)=0 A(4,3)=0 A(4,4)=0 A(4,5)=0
7 Comments
Walter Roberson
on 12 May 2020
See the image 4th row is zero
The question does not ask to make the 4th row zero: the question asks to make the last row zero.
Do not use parenthesis at end of matrix
? Where did Adam use parenthesis at the end of matrix?
Ashim Bhat
on 20 May 2020
for assigning v
v = A(:,2);
for geeting zero values of last row of A
A(4,:) = 0
2 Comments
Adam Danz
on 20 May 2020
Example 1 of this method failing:
A = [1 2 3;
1 2 3;
1 2 3;
1 2 3;
1 2 3;
1 2 3];
A(4,:) = 0;
% Result
A =
1 2 3
1 2 3
1 2 3
0 0 0 % <--- wrong row
1 2 3
1 2 3
Example 2 of this method failing
A = [1 2 3;
1 2 3];
A(4,:) = 0;
% Result
A =
1 2 3
1 2 3
0 0 0
0 0 0 % <--- Now matrix A has 4 rows
MICHAEL
on 12 Jun 2020
v=A(:,2);
A(end, :) = zeros(; size(A,2));
this is the write answer
1 Comment
Walter Roberson
on 12 Jun 2020
that is not valid syntax for using zeros : the semicolon is not correct
Minal Kulkarni
on 30 Jun 2021
A=[1:5; 6:10; 11:15; 16:20];
v=[ A(1,2); A(2,2); A(3,2); A(4,2)]
A=[1:5; 6:10; 11:15; 0,0,0,0,0]
1 Comment
Adam Danz
on 1 Jul 2021
Edited: Adam Danz
on 12 Dec 2021
This is not a solution.
You're overwriting A instead of replacing the last row.
Indexing in the second line is very inefficient.
And the 2nd and 3rd lines assume A has 4 columns.
Please consider taking the Matlab on-ramp.
https://www.mathworks.com/learn/tutorials/matlab-onramp.html
Idowu
on 9 Jan 2023 at 5:45
A = [1:5; 6:10; 11:15; 16:20];
v = A(1:4,2)
A(4, :) = 0
1 Comment
Walter Roberson
on 9 Jan 2023 at 9:03
The requirements are to change the last row to 0, not to change the 4th row specifically to 0.
Your code is the same as the code posted at https://www.mathworks.com/matlabcentral/answers/463130-a-basic-question-of-matrix-indexing-can-t-get-a-proper-output#comment_938612
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!