Replacing all occurrences of a specific value in a matrix with another value

4 visualizaciones (últimos 30 días)
Hi,
Context: I am looking to equalize an image without using histeq. The process requires me to work with large matrices.
Problem: I have two matrices - one that is 291x240 (named im2) and another that is 1x256 (named im3). I want to build a for loop (or use anything else - it is not necessary I use a for loop) that replaces all occurances of a certain value in the im2 matrix (lets call the value n) with the value contained in the nth index of im3.
Given: the values contained in im2 and im3 are between 0-256
So far I have the following loop but it gives me weird results:
for n=1:256
im2(im2==n) = im3(n);
end
An example of the result: there are multiple occurances of 107 in im2; in im3, the 107th index contains 134. Thus, if that for loop worked as I understand it, all occurances of 107 in im2 would become 134, and the loop would loop through to do the same thing for n=1-256.
But instead, the occurances of 107 in im2 are changed to 255 and other numbers to other unexpected values.
Any help is appreciated, thank you.
I surfed the web for similar questions and implemented a number of them, but, I did not get the result I need.

Respuesta aceptada

Matt J
Matt J el 4 de Nov. de 2021
im2=im3(im2);
  3 comentarios
KibreabG
KibreabG el 4 de Nov. de 2021
Hi Matt,
What if im2 and im3 contain values ranging 0-255. but since index numbers must be >0, I would need that code above to map 107 to the 108th index and so on (index must be +1 the value found in im2)?
Matt J
Matt J el 4 de Nov. de 2021
Editada: Matt J el 4 de Nov. de 2021
It would just be,
im2=im3(im2+1);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by