Apply a mask to matrices stored in a cell array
48 views (last 30 days)
Show older comments
Aaron Smith
on 21 Jul 2017
Commented: Aaron Smith
on 26 Jul 2017
I have a mask taken from an image. I need to apply this logical mask to a number of matrices that are the same dimensions as the image. The matrices are stored as the cells in a cell array. I have looked on mathworks at previous questions regarding masks but none appear to help with my particular
0 Comments
Accepted Answer
Sayam Ganguly
on 24 Jul 2017
Hi, I understand that you have a cell array of matrices and you have a mask of the same dimension as the the individual matrices. Now you want to apply these masks to all the matrices in your cell array. I would like to suggest an approach that should help you achieve this. Following is the code sample -
% X is a cell array of dimension 3*4 with matrices of 2*3 dimension
% cellmat is an NN-toolbox function
X = cellmat(3,4,2,3,10);
%mask is the mask to be applied
mask = [true false true ; false true false];
% Loop through the elements of the cell array
for i = 1 : numel(X)
% Apply mask on each of the matrices and store the result back in the cell.
% Here '.*' performs element wise multiplication between the matrices
X{i} = X{i}.*mask;
end
Also you can achieve the same without iteration by using cellfun. Following is the sample code for the same -
X = cellfun(@(x) x.*mask,X,'UniformOutput', false)
Give either approach a try and see which suits your need better. Hope this helps!
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!