MATLAB Answers

Does logical indexing have a direct inverse?

36 views (last 30 days)
Michael on 10 Dec 2012
Say I have some region-of-interest filter X, and I use B = A(X~=0) to only extract the values of A where X~=0, is it then possible to retrieve A from B and X?
I'm trying to think of a clever way to do it but coming up a little short!
Thanks for any suggestions,
  1 Comment
Martin Herrerias Azcue
Martin Herrerias Azcue on 8 Nov 2019
I come accross the same problem often enough that I wrote a function for it. It's really a matter of OCD, but I find:
A = revertfilter(B,f);
cleaner than:
A = zeros(size(B),'like',B);
A(f) = B;
Hope it helps.
function A = revertfilter(B,filter,dim,missing)
% A = REVERTFILTER(B,FILTER,DIM,MISSING) - Revert logical-indexing of A by FILTER upon dimension
% DIM, filling any ~FILTER slices with MISSING.
% E.g. if B = C(:,:,FILTER,:), A = REVERTFILTER(B,FILTER,3,NaN) will return a size(C) array,
% such that all C(:,:,FILTER,:) == A(:,:,FILTER,:), and for which all A(:,:,~FILTER,:) == NaN.
% A = REVERTFILTER(B,FILTER,[],MISSING) - Revert logical-indexing B = A(FILTER), setting the size
% of A from FILTER. For this to work DIM must be missing/empty, and nnz(FILTER) == numel(B).
% E.g. C = rand(1,2,3); FILTER = C > 0.5; B = C(FILTER); A = REVERTFILTER(B,FILTER)
% (c) copyleft, Martin Herrerías 2019
if nargin < 3, dim = []; end
if nargin < 4, missing = 0; end
assert(islogical(filter),'REVERSEFILTER only works for logical indexing');
if isnumeric(B), missing = double(missing);
elseif islogical(B), missing = missing > 0;
assert(isequal(class(B),class(missing),'Class of MISSING must match B'));
if nnz(filter) == numel(B) && isempty(dim)
A = emptyarray(size(filter),missing,B(1));
A(filter) = B;
elseif isvector(filter)
if isempty(dim)
if size(filter,2) > 1, dim = 2; else, dim = 1; end
assert(size(B,dim) == nnz(filter),'nnz(FILTER) ~= size(B,DIM)');
sz = size(B);
sz(dim) = numel(filter);
A = emptyarray(sz,missing,B(1));
% Place enough colons before and after dim for A(..,filter,..) = B to work
args = [repmat({':'},1,dim-1),{filter},repmat({':'},1,numel(sz)-dim)];
A(args{:}) = B;
error('Unrecognized syntax/arguments');
function A = emptyarray(sz,missing,b)
% Not sure if this is worth the trouble: it might just be faster to use repmat(missing,sz),
% the idea is that the type of A is still set, whenever possible, by B
if isnan(missing), A = NaN(sz,'like',b); return; end
switch missing
case 0, A = zeros(sz,'like',b);
case 1, A = ones(sz,'like',b);
case true, A = true(sz);
case false, A = false(sz);
case Inf, A = Inf(sz,'like',b);
case -Inf, A = -Inf(sz,'like',b);
otherwise, A = zeros(sz,'like',b); A(:) = missing;

Sign in to comment.

Answers (4)

Sean de Wolski
Sean de Wolski on 10 Dec 2012
A(X~=0) = B
Sean de Wolski
Sean de Wolski on 10 Dec 2012
No, the location data is required and it comes from X.
Also, you will never know about the values in A that were not extracted from X or the size of A. Thus you need the index to go in reverse.

Sign in to comment.

Walter Roberson
Walter Roberson on 10 Dec 2012
No, it is not possible.
Suppose for example X was a matrix the same size as A, and is 1 for the top-left quadrant and 0 elsewhere. B = A(X~=0) would then retrieve values from the top left quadrant and would have no information about anything elsewhere in A. X also has no information about anything in A. Therefore, if you have only X and B, you cannot reconstruct the information that was in the other three quadrants of A.
My guess about what you actually want to do is:
A(X ~= 0) = B;

Matt Fig
Matt Fig on 10 Dec 2012
Edited: Matt Fig on 10 Dec 2012
Simple example:
A = randperm(4);
X = [0 1 0 1]
B = A(X~=0)
clear A
Now just look at X and B. By looking at just those two arrays, it is not even in principle possible to tell exactly what A was! This despite the fact that we know all of the elments of A! In a more general case, the problem is only worse. If you, as a human being, cannot tell what the original A was with such a simple example, how in the world could MATLAB tell?

Image Analyst
Image Analyst on 10 Dec 2012
Michael: As the others have said, you can't get back ALL of a from only the part you extracted - you'd need the complete a to do that. However you extracted a chunk of "a" into a 1D column vector, and it is possible to put that back into a 2D reconstructed "a" where the extracted part is the same, but the unextracted part is zero (or any other number you want). Here's some demo code. See if it's what you were thinking.
% Generate sample data.
a = magic(8)
% Make x a mask in the upper left quadrant.
x = false(size(a)); % Initialize.
x(1:4, 1:4) = true % Mask of upper left quadrant
% Extract masked a into b.
b = a(x~=0) % This will be a 16 by 1 column vector.
% We can't get back ALL of a, without using a, but we CAN get back the a that we extracted
% back into the original 2D shape with zeros (or any constant) elsewhere.
a_reconstructed = zeros(size(x)); % Initialize a new "a" with 2D matrix of 0's.
% Find the linear indices that we used to extract elements of a.
linearIndexes = find(x~=0)
% Use them to assign those elements of a reconstructed a
% from the elements in b.
a_reconstructed(linearIndexes) = b(:)

Community Treasure Hunt

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

Start Hunting!