Retrieve original index from a masked array

Hi, I am not sure if it is even possible, but suppose I have an array A and a mask m, and I use some function (e.g. min()) to find the index of an element in the masked function, is there a way to backtrack that element back to the original unmasked array?
In other words:
[~,i] = min(A(m)); % A is an array, m is a logical array mask
Now I want to find the index j such that A(j) is the same element as (A(m))(i)
Thanks,

 採用された回答

KSSV
KSSV 2020 年 7 月 28 日

1 投票

Repalce the other elements in A with NaN other than mask.
B = A ;
B(~m) = NaN ;
[~,i] = min(B); % now the index will be of A

4 件のコメント

Raphael Alhadef
Raphael Alhadef 2020 年 7 月 28 日
Perfect, thanks!
KSSV
KSSV 2020 年 7 月 28 日
Thanks is accepting/voting the answer.. 😊
Image Analyst
Image Analyst 2020 年 7 月 28 日
Be aware that if the min pixel value in the masked array B occurs in more than one location, i will only give you the first location it encounters, not all of them.
Raphael Alhadef
Raphael Alhadef 2020 年 7 月 30 日
Yes, I also realized that this solution returns 1 if mask is all 0's which is an unintended consequence, but for my specific needs it works fine.
Thanks for the feedback!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 7 月 28 日

1 投票

Here is a way to find out where all the min values within the masked region occur in your original masked image.
% Create a small image.
grayImage = uint8(randi(9, 20, 20))
% Create a mask that's true from rows 5-15 and columns 2-18.
mask = false(size(grayImage));
mask(5:15, 2:18) = true
% Mask the image.
maskedImage = grayImage .* uint8(mask) % Erase outside the mask.
% Get the min value of grayImage within the mask:
minValue = min(maskedImage(mask))
% Find all the rows and columns where this occurs
occurrenceMap = (maskedImage == minValue) .* mask % An image
[rows, columns] = find(occurrenceMap) % An N-by-2 list of all row,column combinations where the min occurs

1 件のコメント

Raphael Alhadef
Raphael Alhadef 2020 年 7 月 30 日
Thanks! This is the original solution I thought of but it is a bit slower and I was looking for something faster.

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

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

製品

リリース

R2018b

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by