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

101 views (last 30 days)
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
Walter Roberson on 11 Jan 2023
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.

Sign in to comment.

Accepted Answer

Adam Danz
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;

More Answers (7)

amjad khan
amjad khan on 3 Apr 2020
Edited: DGM on 4 Mar 2023
A = [1:5; 6:10; 11:15; 16:20];
v=A(:,2) % assigning variable v to the second column of matrix "A"
A(4,:)=0 % changing all the elements of row 4 to zeros
  1 Comment
Adam Danz
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.

Sign in to comment.


Badal Bhardwaj
Badal Bhardwaj on 12 May 2020
Edited: DGM on 4 Mar 2023
Answer is
A(4,1)=0
A(4,2)=0
A(4,3)=0
A(4,4)=0
A(4,5)=0
  7 Comments
Adam Danz
Adam Danz on 12 May 2020
This isn't a correct answer to the question. The question asks how to change values of the last row of a matrix to zero, not the 4th row.
Walter Roberson
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?

Sign in to comment.


Ashim Bhat
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
Walter Roberson
Walter Roberson on 20 May 2020
That assigns to the 4th row of A, which might not be the last row of A.
Adam Danz
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

Sign in to comment.


Imane Tahar
Imane Tahar on 14 Nov 2020
A = [1:5; 6:10; 11:15; 16:20];
v= A(:,2)
A(end,:)= 0

Minal Kulkarni
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
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.

Sign in to comment.


Muniba
Muniba on 3 Sep 2023
A = [1:5; 6:10; 11:15; 16:20];
v=A(1:end,2)
v = 4×1
2 7 12 17
A(end:4,1:end)=0
A = 4×5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 0 0 0
  2 Comments
DGM
DGM on 3 Sep 2023
Edited: DGM on 3 Sep 2023
This is arguably not the correct way, or at least not a robust way. Simpler and more robust answers have already been given.
It's unnecessary to do this. I think it just adds clutter that can make larger expressions less readable, but I suppose other opinions can exist.
%v=A(1:end,2) % is the same as
v=A(:,2) % just using :
The question asks to set the last row to zero. Your answer sets the fourth row to zero. There are four rows, so it's the same, right? Well, this only works if there are exactly four rows. If there are more than four rows, your method will fail silently, assigning nothing to zero. If there are fewer than four rows, it will set the last row (whichever that is) to zero, and add extra rows to the array such that it has four rows. If A had 3 rows, it will wind up with two rows of zeros.
%A(end:4,1:end)=0 % selects row 4, but only if there are exactly 4 rows!
A(end,:)=0 % select the _last row_!
There was no reason to add this extra complication and all its problems. The generalized solution is simpler to write and simpler to read.

Sign in to comment.


Haris
Haris on 19 Feb 2024
Variable A must be of size [4 5]. It is currently of size [5 5]. Check where the variable is assigned a value.
  2 Comments
Dyuman Joshi
Dyuman Joshi on 19 Feb 2024
@Haris, how exactly is your comment, which you have mistakenly posted as an answer, relate to this thread?
DGM
DGM on 20 Feb 2024
Edited: DGM on 20 Feb 2024
Nobody has given a clear description of the assignment, and almost all of the answers to this thread are either redundant, wrong, or both. As far as anyone here knows, there is no requirement that A is any particular size. In fact, it seems like a terrible idea to teach students to presume that inputs are a particular mystery size and then write code that relies on that presumption.
Of course, being a terrible idea makes it plausible that someone actually wrote an assignment like that, so if you are in a position to give a better description of the assignment, then by all means, go ahead.
Just because someone set the grader up so that it accepts bad code that panders to an overly simplistic test doesn't mean anybody should call the bad code "correct".

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!