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

采纳的回答

Sayam Ganguly
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!
  1 个评论
Aaron Smith
Aaron Smith 2017-7-26
Thanks a million Sayam.
In you're sample code, the cellfun code without iteration, is the small x a typo? I can't tell what value this represents. I tried the first method, with the iteration. It generates an error once I added to my GUI stating that Cell contents reference from a non-cell array object.
The code is here:
handles = guidata(hObject);
Folder = uigetdir('C:\Users\c13459232\Documents\MATLAB'); % selecta folder from which to select files
File = uigetfile(Folder); % select a file
Image = imread(File); % Read image
[rows, columns, numberOfColorChannels] = size(Image);
imshow(Image); % generate the image in a figure
again = true; % again is true by default. the user must select stop to make it false
finalImage = false(rows, columns);
while again
thisRegion = roipoly;
% Add this in to the main one
finalImage = finalImage | thisRegion;
promptMessage = sprintf('Do you want to add another region?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes', 'No', 'Yes');
if strcmpi(buttonText, 'No')
again = false;
end
labeledImage = logical(finalImage);
end
X = handles.finishCell;
mask = labeledImage;
for i = 1 : numel(X)
X{i} = X{i}.*mask;
end
handles.X = X{i};
guidata(hObject, handles);
Here, handles.finishCell is the cell array created previously in my GUI. it is a 1 x 120 cell array and each cell contains a 1024 x 1024 matrix. The Error:
Cell contents reference from a non-cell array object.
Error in hopefully_the_last_window>pushbutton3_Callback (line 185)
X{i} = X{i}.*mask;
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in hopefully_the_last_window (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)hopefully_the_last_window('pushbutton3_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Call
I'm not sure which non cell array object this is referring to.

请先登录,再进行评论。

更多回答(0 个)

产品

Community Treasure Hunt

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

Start Hunting!

Translated by