Change the sign of a particular element in a matrix

조회 수: 7 (최근 30일)
Pranjal Pathak
Pranjal Pathak 2012년 12월 28일
Hi, I have a problem in the following matrix: A=[ -1 1 0 0 0 0; 0 -1 1 0 0 0 ; 0 0 -1 1 0 0; 0 0 0 0 -1 1 ; 0 0 0 0 0 -1 ; 0 0 0 0 0 0];
In the above matrix, I need to change only the element -1 by +1, while others remaining the same. For, small matrix, it is possible by defining the row and column elements but for large matrix, is there any way of doing it programatically in matlab as the dimension of the matrix may be larger than this. Please help me in this regard.
Thanking You!

채택된 답변

Muruganandham Subramanian
Muruganandham Subramanian 2012년 12월 28일
편집: Jan 2012년 12월 28일
find(A==-1)=1
[EDITED, Jan, copied from comments] Better: A(A==-1)=1;
or
A=abs(A)
  댓글 수: 3
Pranjal Pathak
Pranjal Pathak 2013년 1월 9일
Thanks for the answer,it worked out!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2012년 12월 28일
What do you mean change it by +1? Do you mean change it TO +1, or change it by +1 by adding +1 to the -1 so that you get 0? Here's three different ways to do the two possible meanings of your question:
A = [ -1 1 0 0 0 0; 0 -1 1 0 0 0 ; 0 0 -1 1 0 0; 0 0 0 0 -1 1 ; 0 0 0 0 0 -1 ; 0 0 0 0 0 0]
A(A == -1) = 1
A = [ -1 1 0 0 0 0; 0 -1 1 0 0 0 ; 0 0 -1 1 0 0; 0 0 0 0 -1 1 ; 0 0 0 0 0 -1 ; 0 0 0 0 0 0]
A(A == -1) = 0
A = [ -1 1 0 0 0 0; 0 -1 1 0 0 0 ; 0 0 -1 1 0 0; 0 0 0 0 -1 1 ; 0 0 0 0 0 -1 ; 0 0 0 0 0 0]
elementsToChange = A == -1;
A(elementsToChange) = A(elementsToChange) + 1
  댓글 수: 2
Shaun VanWeelden
Shaun VanWeelden 2012년 12월 28일
Image Analyst, your always so thorough in your answers, I love it
Jan
Jan 2012년 12월 28일
@Shaun: You can vote this answer, if you find it useful.

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by