Convert from binary values ​​to the original array values

Given a 2D array that has been converted to binary, for some binary array index values, how do you find the corresponding values ​​in the original?

Maybe something to use ind2sub?

+3
source share
2 answers

No, you can index directly.

%# create some test data
m = magic(4);
%# make binary image
bw = m>10;

%# read values from m
values = m(bw);

%# alternatively, if you have linear indices (as found via find)...
linIdx = find(bw);
%# ...you can use that instead
values = m(linIdx);
+5
source

You can save a 2D structure using element multiplication.

m = magic(4);
bw = m>10;
m .* bw
+1
source

Source: https://habr.com/ru/post/1744367/


All Articles