Apply a mask to matrices stored in a cell array
20 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aaron Smith
el 21 de Jul. de 2017
Comentada: Aaron Smith
el 26 de Jul. de 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 comentarios
Respuesta aceptada
Sayam Ganguly
el 24 de Jul. de 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!
Más respuestas (0)
Ver también
Categorías
Más información sobre Image Preview and Device Configuration en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!