Apply a mask to matrices stored in a cell array
45 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Sayam Ganguly
2017-7-24
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!
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!