Apply a mask to matrices stored in a cell array
조회 수: 19 (최근 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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!