4x4 matrix with numbers from 1 to 4 in specific order

조회 수: 8 (최근 30일)
Dobs
Dobs 2021년 10월 23일
댓글: Dobs 2021년 10월 23일
Hi,
I want to create the following matrix:
M = 1 2 3 4
0 0 0 3
0 0 0 2
0 0 0 1
I'm only supposed to use the vector r = 1:4 and no other functions. This is what I got so far:
M=r
M(1:4, 4)=r
But this results in
M = 1 2 3 1
0 0 0 2
0 0 0 3
0 0 0 4
So now I'm stuck. Does anybody know how to "reverse" the last column so that it reads 4 3 2 1 instead of 1 2 3 4?
All help is greatly appeciated!
Cheers,
Dobs

채택된 답변

Image Analyst
Image Analyst 2021년 10월 23일
You need to put r in in reverse order, so assuming you're not allowed to use flipud(r), try:
r = 1:4
M(1:4, 4)= r(end : -1 : 1);
Then you need to set row 1:
M(1, ......................

추가 답변 (1개)

John D'Errico
John D'Errico 2021년 10월 23일
편집: John D'Errico 2021년 10월 23일
Hint: What does flip do? Could you use it to "flip" the order of those numbers?
  댓글 수: 1
Dobs
Dobs 2021년 10월 23일
I think we're not allowed to use it since we're only supposed to use the vector r and no other functions. But I just tried it and it works too! Will keep this in mind for the next time I need to reverse the numbers in a vector. Thank you so much for your help!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by