## Locating variables in a matrix and replacing them

### Locating variables in a matrix and replacing them

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

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

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
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

> 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/

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

6. digital