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

2 ビュー (過去 30 日間)
KibreabG
KibreabG 2021 年 11 月 4 日
編集済み: Matt J 2021 年 11 月 4 日
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 日
im2=im3(im2);
  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)?
Matt J
Matt J 2021 年 11 月 4 日
編集済み: Matt J 2021 年 11 月 4 日
It would just be,
im2=im3(im2+1);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by