How to replace some values in a matrix?

I have x=16 * 16 matrix of double values. i need to figure out consecutive values of 'x' which are less than 150 and replace them with the original value plus(+) 8.

 Respuesta aceptada

Stephen23
Stephen23 el 1 de Mayo de 2017
Editada: Stephen23 el 1 de Mayo de 2017
Just use a logical index:
>> x = randi(200,16,16);
>> idx = x<150;
>> x(idx) = x(idx)+8;
Very basic MATLAB operations, such as logical indices, as introduced in the Getting Started tutorials, which are highly recommended for all beginners:

3 comentarios

aditya sahu
aditya sahu el 1 de Mayo de 2017
Editada: aditya sahu el 1 de Mayo de 2017
Dear Stephen Cobeldick, your code is woking, but,it is not working for the consecutive values.i mean to say is, suppose two consecutive values are 180 and 56, then it should skip both. But according to your code it makes 56+8=64.
Stephen23
Stephen23 el 1 de Mayo de 2017
Editada: Stephen23 el 1 de Mayo de 2017
@aditya sahu: please explain the requirements with some example input and output data, because it is not clear to me what exactly you want. Why is 56 ignored, even though it is less than 150?
Image Analyst
Image Analyst el 1 de Mayo de 2017
How are you defining consecutive? MATLAB goes row by row (down rows), then over to the next column and down its rows. Is that your definition? If so, then is the last element in column 2 consecutive with the first element in column 3?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Processing Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 1 de Mayo de 2017

Comentada:

el 1 de Mayo de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by