Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Change the sign of a particular element in a matrix

Asked by Pranjal Pathak on 28 Dec 2012

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!

0 Comments

Pranjal Pathak

Products

No products are associated with this question.

2 Answers

Answer by Muruganandham Subramanian on 28 Dec 2012
Edited by Jan Simon on 28 Dec 2012
Accepted answer
find(A==-1)=1

[EDITED, Jan, copied from comments] Better: A(A==-1)=1;

or

A=abs(A)

3 Comments

Azzi Abdelmalek on 28 Dec 2012

find(A==-1)=1 will not change A. That will create a variable find

Muruganandham Subramanian on 28 Dec 2012

Sorry..Azzi Its

A(A==-1)=1;
Pranjal Pathak on 9 Jan 2013

Thanks for the answer,it worked out!

Muruganandham Subramanian
Answer by Image Analyst on 28 Dec 2012

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 Comments

Shaun VanWeelden on 28 Dec 2012

Image Analyst, your always so thorough in your answers, I love it

Jan Simon on 28 Dec 2012

@Shaun: You can vote this answer, if you find it useful.

Image Analyst

Contact us