Locating variables in a matrix and replacing them

Locating variables in a matrix and replacing them

Post by Elai » Fri, 18 Apr 2003 05:24:24



I have a matrix 300 x 600 and I want to search each element of it and
replace any value less than avg with this value avg, I have tried two
ways of doing this:

For j = 300:600,
  If(mymatrix (j) >= avg),
    Mymatrix(j) = avg;
  End
End

The other way:

While (row <= 300)
  While (col <= 600),
     If( (mymatrix (row:col) ) >= avg),
       Mymatrix(row:col) = avg;
     End
   Col = col +1;
   End
  Row =row +1;
End

When I run these no errors appear, I look at my data in my matrix and
there is till values greater that avg, does anybody have any idea
where im going wrong?

Thanks!

 
 
 

Locating variables in a matrix and replacing them

Post by Michael Robbin » Fri, 18 Apr 2003 05:38:25


Quote:> I have a matrix 300 x 600 and I want to search each element of it
and
> replace any value less than avg with this value avg,

mymatrix(mymatrix<avg)=avg;

Quote:> When I run these no errors appear

Then you probably are not running it.

 
 
 

Locating variables in a matrix and replacing them

Post by Dan Hensle » Fri, 18 Apr 2003 05:44:00


Doing it in loops is the wrong way.  Try this instead:

Mymatrix(mymatrix<avg)=avg;

You might want to look at solution 1109 at www.mathworks.com/support, and
you should also read
http://www.mathworks.com/company/digest/sept01/matrix.shtml

Now let's look at your scenarios:


> I have a matrix 300 x 600 and I want to search each element of it and
> replace any value less than avg with this value avg, I have tried two
> ways of doing this:

> For j = 300:600,
>   If(mymatrix (j) >= avg),
>     Mymatrix(j) = avg;
>   End
> End

Several things wrong here:

1)  mymatrix and Mymatrix are two separate matrices.  Matlab is case
sensitive in this regard.
2)  The code 300:600 creates a vector with numbers 300-600 in it.  Looping
over 300-600 will loop over the 300th to 600th elements in your matrix,
which is 300x600=180000 elements.  So you're missing most of the matrix
with this loop
3)  Your description says that you're trying to replace values less than
avg with avg, but your code is replacing values greater than or equal to
avg with avg.

Quote:> The other way:

Did you forget to initialize row and col?

row=1;
col=1;

Quote:> While (row <= 300)
>   While (col <= 600),
>      If( (mymatrix (row:col) ) >= avg),
>        Mymatrix(row:col) = avg;
>      End
>    Col = col +1;
>    End
>   Row =row +1;
> End

You have the wrong index notation.  Use mymatrix(row,col) instead.  Also
you have the problem of mymatrix and Mymatrix being different.  And you're
replacing values greater than or equal to instead of less than.

Dan

Quote:> When I run these no errors appear, I look at my data in my matrix and
> there is till values greater that avg, does anybody have any idea where
> im going wrong?

> Thanks!

 
 
 

Locating variables in a matrix and replacing them

Post by Peter Boettche » Fri, 18 Apr 2003 05:48:48



> I have a matrix 300 x 600 and I want to search each element of it and
> replace any value less than avg with this value avg, I have tried two
> ways of doing this:

> For j = 300:600,
>   If(mymatrix (j) >= avg),
>     Mymatrix(j) = avg;
>   End
> End

First, a 2D matrix needs 2 subscripts.  mymatrix(200, 100) for
example.  Second, you want j to run from 1 to 300 OR 600, not 300 to
600.  The loop implementation would look like this:

for i = 1:300
    for j = 1:600
        if(mymatrix(i,j) > avg)
            mymatrix(i,j) = avg;
        end
    end
end                      

Or, any 2D matrix can linearly addressed, in this case 1 to 300*600.

for j = 1:300*600
    if(mymatrix(j) >= avg)
   % etc

This being MATLAB, the non-loop solution is simply:

mymatrix(mymatrix > avg) = avg;

Might I suggest going through some of the materials in the Getting
Started book?

--

MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

 
 
 

1. Q: replacing a string variable with it's expression??

Hi all!
Is the following possible to do without reinventing the wheel?

If i have

    variable1='k1+k2'
    variable2='k3*k4'

now having the two variables defined as strings as above, can i later make
the string expression

    'variable1/variable2'

or any other math expression composed of strings become a new combined
string like

    '(k1+k2)/(k3*k4)'

in brief, can i somehow replace any (string-)variable in an expression with
it's corresponding string-expression??

cheers
Robert

2. Loaned Phones/Courtesy Phones (Was AOS Payphones @#$%%%$#)

3. Replace NaNs, only in part of matrix

4. FS xircom 56k global access modem

5. Replacing multiple rows of a matrix.

6. digital

7. replacing some part of the matrix with zeros

8. Audio CD Sound Missing on SBPro2

9. Units ands positioning

10. Replacing a Matrix Values

11. replace NaNs in matrix

12. GPS/INS intgrated system

13. gps/ins INTEGRATION