Question:

Matlab question on Matrices?

by  |  earlier

0 LIKES UnLike

Hey everyone,

I have a question, I am playing around with Matlab for the first time. I am just wondering if I have a matrix of -1s and 1s. how could I change the values of 1s to -1s and vise versa.

example I have

A = [ -1 1 -1; 1 1 -1]

I want to change them to have

B = [1 -1 1; -1 -1 1]

Thanks in advance

 Tags:

   Report

1 ANSWERS


  1. I would recommend using a for loop  with a nested " if " statment in order to change the values .... since you only have 1's and -1's in your file  this is not neccesary ,but in the future if you are trying to find a certain value in a matrix and change it to another value this is very useful.

    you say the following:

    A=[ -1 1 -1; 1 1 -1]

    so you should do the following:

    [rows,cols]=size(A);

    for n =1:cols
         for m=1:rows
            if A(m,n)== -1
               A(m,n) = 1
            else A(m,n)= -1

    The above lines of code will check every value in your matrix for the conditions of the if statement and if it finds that A= -1 then it will replace that value in the matrix with a 1 and vice versa ,but if you wanted to just create another matrix then use a diff variable.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.