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!