How do you divide each row by its first nonzero element?

How you would divide each row by its first nonzero element? For example, if I have a matrix A=[1 9 7;0 2 4;0 0 5], I want it to be [1 9 7;0 1 2; 0 0 1] where the diagonals of the matrix are 1s. As you can see, I want the first row and all of its columns to be the same. Would it also be possible to have the code for this problem be 1 line?

댓글 수: 2

What do you mean that you "want... all of its columns to be the same"?
I meant to say the elements of the first row of the matrix to be the same. Anyways, you answered my questions already and it works for me. Thank you!

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

 채택된 답변

Stephen23
Stephen23 2017년 10월 30일
>> A = [4,9,7;0,2,4;0,0,5];
>> B = bsxfun(@rdivide,A,diag(A));
>> B(1,:) = A(1,:)
B =
4 9 7
0 1 2
0 0 1

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2017년 10월 30일

댓글:

2017년 10월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by