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

3 次查看(过去 30 天)
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.

采纳的回答

Matt J
Matt J 2021-11-4
  3 个评论
KibreabG
KibreabG 2021-11-4
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)?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by