Is there is a way I can select the minimum value from a matrix, and replace all numbers surrounding this minimum value (3 cells) by a specific number

I want to select this minimum value cell, and anything within a radius of ‘less than or equal to’ three cells to be replaced with a specific number such as 99 (including the selected minimum cell).
I have uploaded a pdf of what i am trying to accomplish. Sorry for any inconvenience and thank you.

 Accepted Answer

[~,imin]=min(x(:)); % location of min (linear
[im,jm]=ind2sub(size(x),imin); % convert to subscripts
x(im-1:im+1,jm-1:jm+1)=99; % set a square around that location

6 Comments

when i do the 'set square around that location' i get an error saying "Error: Unbalanced or unexpected parenthesis or bracket."
@Ruby: The message is clear: There is a typo in the code line. I suggest to search for it by your own. You will find out, that there is a wrong semicolon in this line, which should be a comma.
Ah yes thank you. I didn't realise this error and i thought it was the 'parenthesis' as i dont know what parenthesis means.
The punctuation marks '()' are parentheses...I agree the error message is indeed unclear given the actual error. Sometimes when the parser gets confused by an unexpected character it just runs on until something else breaks and it's that subsequent error that actually gets flagged instead of the actual cause.
Here the semi-colon that would imply a second row of an array is apparently assumed to be intended and that would imply needing square brackets around that expression instead of parens hence the message spit out when it found the trailing ')'. If it had thought instead it was parsing a 2D array indexing position, then it might have found and alerted on the semi-colon. Well, let's see...
>> x=zeros(5);
>> im=2;jm=2;
>> x(im-1:im+1;jm-1:jm+1)=99;
x(im-1:im+1;jm-1:jm+1)=99;
|
Error: Unbalanced or unexpected parenthesis or bracket.
>>
Well, nope--even if x is preallocated as an array the same error message--that's the best it seems Matlab can do in this case.
Note, however, it's in the middle of the expression where the '|' is pointing that indicates it's somewhere inside the expression that the parser did get confused that's at least some help in looking for the problem.
Regarding the 'setting a square perimeter' around the min value, if the min value is in either column 1 or row 1, it shows an error message. This is because there are no numbers around the edge of the matrix so it cant set them as 99. Is there any way around this error?
You'll have to write logic to handle the condition; there's no automagic way that I can think of. Wrapping the bounds expressions inside min and max is one way can do that so that, for instance, the LHS x-bound becomes
max(im-1,1)
Similar logic w/ proper limits for the other directions should be obvious extension. The end keyword is invaluable in such instances for upper bound values, btw...

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 9 Nov 2013

Commented:

dpb
on 11 Nov 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!