A basic question of matrix indexing can't get a proper output
이전 댓글 표시
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
Walter Roberson
2019년 5월 20일
The last row of A is not row 5.
Debaditya Chakraborty
2019년 5월 20일
Ayush Khandelwal
2020년 5월 12일
Full answer dijiye
Gaurav Saini
2020년 5월 16일
It's not run on matlab
Walter Roberson
2020년 5월 16일
The posted code runs without difficulty on MATLAB. It is, however, not correct code for the Question. The Question requires that the last row of A be changed to 0, but the code posted here instead adds a new row.
Ashim Bhat
2020년 5월 20일
편집: Ashim Bhat
2020년 5월 20일
FOR ASSIGNING V
V = A(: , 2)
FOR GETTING LAST ROWS AS ZEROS
A(4, :) = 0
Walter Roberson
2020년 5월 20일
No, A(4,:)=0 changes the 4th row of A to 0 no matter how many rows A has.
Adam Danz
2020년 5월 20일
I'm baffled by the confusion in this thread. It's really quite simple.
a(end,:)
selects the last row of 'a'.
a(end+1,:) = [. . .]
adds a row to the end of 'a'.
It's that simple.
Walter Roberson
2020년 5월 20일
"Given matrix A" and the example matrix happens to have 4 rows so people are customizing the code to the four row case.
Lokesh Belekar
2020년 5월 22일
V= A(1:4,2); A (4,:) = 0
///It's correct Answer.
Walter Roberson
2020년 5월 22일
No, Lokesh, the requirement is "Afterwards change each element of the last row of A to 0." You are changing the 4th row of A, not the last row of A. Suppose A has 7 rows, then you would have to change the 7th row not the 4th.
Tridisha Gogoi
2020년 7월 16일
A = [1:5; 6:10; 11:15; 16:20];
v=A(:,2)
A(4:end,:)=0
Walter Roberson
2020년 7월 16일
No, Tridisha, the requirement is not to set all rows from #4 onwards to zero, only to set the last row to 0.
Mohd Suhel Ansari
2020년 9월 4일
A = [1:5; 6:10; 11:15; 16:20];
v=A(:,2)
A(4,[1 2 3 4 5])=0;
Walter Roberson
2020년 9월 5일
No, Mohd Suhel Ansari, the requirement is not to set row #4 to zero: the requirement is to set the last row to zero.
SHUBHAM HINGE
2021년 1월 8일
A = [1:5; 6:10; 11:15; 16:20];
v=A(1:end,2)
A(4,1:end) = 0
Adam Danz
2021년 1월 8일
- "1:end" can and should be replaced by A(:,2) as my answer demonstrates.
- avoid hard-coding indices in "A(4,1:end)". Instead, use A(end,:)=0 as my answer demonstrates.
khaula
2022년 9월 14일
it works
Vishal
2023년 1월 11일
make it -
......
A(5, :)= zeros(1,5);
Walter Roberson
2023년 1월 11일
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.
채택된 답변
추가 답변 (7개)
amjad khan
2020년 4월 3일
편집: DGM
2023년 3월 4일
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
Adam Danz
2020년 4월 3일
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.
Badal Bhardwaj
2020년 5월 12일
편집: DGM
2023년 3월 4일
Answer is
A(4,1)=0
A(4,2)=0
A(4,3)=0
A(4,4)=0
A(4,5)=0
댓글 수: 7
Walter Roberson
2020년 5월 12일
No, that only works if A happens to be 4 rows. The question requires that the last row of A be replaced with zeros -- no matter what size A is.
Badal Bhardwaj
2020년 5월 12일
I have do this my answer is correct At the end of these 5 steps the last row of A is 0 Firstly try then comment
Example of it not working
A = [1 2 3 4 5;
1 2 3 4 5;
1 2 3 4 5;
1 2 3 4 5;
1 2 3 4 5;
1 2 3 4 5];
A(4,1)=0
A(4,2)=0
A(4,3)=0
A(4,4)=0
A(4,5)=0
Result
A =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
0 0 0 0 0
1 2 3 4 5
1 2 3 4 5
If you need help understanding why your solution only works when A has 4 rows, please let us know.
Also, your 5 lines of code can be reduced to 1 line (see my answer above).
Badal Bhardwaj
2020년 5월 12일
See the image 4th row is zero
Badal Bhardwaj
2020년 5월 12일
Do not use parenthesis at end of matrix I know this Is lengthy code but definitely correct
Adam Danz
2020년 5월 12일
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
2020년 5월 12일
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
2020년 5월 20일
for assigning v
v = A(:,2);
for geeting zero values of last row of A
A(4,:) = 0
댓글 수: 2
Walter Roberson
2020년 5월 20일
That assigns to the 4th row of A, which might not be the last row of A.
Adam Danz
2020년 5월 20일
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
Imane Tahar
2020년 11월 14일
A = [1:5; 6:10; 11:15; 16:20];
v= A(:,2)
A(end,:)= 0
Minal Kulkarni
2021년 6월 30일
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
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.
A = [1:5; 6:10; 11:15; 16:20];
v=A(1:end,2)
A(end:4,1:end)=0
댓글 수: 2
Muniba
2023년 9월 3일
This is the correct way.
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.
Haris
2024년 2월 19일
0 개 추천
Variable A must be of size [4 5]. It is currently of size [5 5]. Check where the variable is assigned a value.
댓글 수: 2
Dyuman Joshi
2024년 2월 19일
@Haris, how exactly is your comment, which you have mistakenly posted as an answer, relate to this thread?
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".
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!