Reputation: 175
I have a cell 5x5 and each cell has 100x100 single data. I want to calculate the mode in each matrix 100x100 and then do some operations that depends on the mode. How Can I do this?
Each cell has 100x100 data like this:
I have this code that calculate the mode on each cell, now I want to compare each value on 100x100 matrix with corresponding cell mode.
I = imread('DSM.tif');
c = mat2cell(I, [100,100,100,100,100], [100,100,100,100,100])
for i=1:5
for j=1:5
mode_cell = mode(c{i,j}(:))
end
end
I did this code:
modes = cellfun(@(x) mode(x(:)), c, 'UniformOutput', false);
modes = cell2mat(cellfun(@(x) mode(x(:)), c, 'UniformOutput', false));
for i = 1 :5
for j =1 :5
for i2=1 :100
for j2=1 :100
cell = c{i,j};
if cell(i2,j2)<modes(i,j)
teste(i,j)=0;
else
teste(i,j)=1;
end
end
end
end
end
But with this code the matrix teste is only 100x100. And I want to attach all testes matrix, and in the end I want an 500x500 matrix. How can I do this?
Upvotes: 2
Views: 67
Reputation: 73166
Condensed solution (+)
:
%// data
I = imread('DSM.tif');
c = mat2cell(I, [100,100,100,100,100], [100,100,100,100,100])
%// 5x5 cell matrix with 100x100 data entries
%// solution
teste = cell2mat(cellfun(@(x) bsxfun(@ge, x, mode(x(:))), c, 'UniformOutput', false));
%// 500x500 logical matrix
Explanation:
As previously (see the previous answer below), make use of the cellfun
command, but include the 'UniformOutput', false'
name-value pair such that the resulting modes
is a 5x5
cell matrix (rather than a value matrix). After computing the modes, use a subsequent cellfun
call to compute the logicals as per your specifications.
%// data
I = imread('DSM.tif');
c = mat2cell(I, [100,100,100,100,100], [100,100,100,100,100])
%// 5x5 cell matrix with 100x100 data entries
%// modes: 5x5 cell matrix with single value entries (mode)
modes = cellfun(@(x) mode(x(:)), c, 'UniformOutput', false);
%// teste: 5x5 cell matrix with 100x100 logical (0/1) entries
teste = cellfun(@ge, c, modes, 'UniformOutput', false);
The logicals in teste
describes, for each value in each 100x100
data block, if the value is less than the mode value for the corresponding block:
0 : values in block less than block mode,
1 : values greater or equal to block mode.
Finally, if you want to convert the 5x5
cell array teste
(with each cell holding 100x100
logical entries) to a single 500x500
logical matrix, you can make use of the cell2mat
command, applied on cell matrix teste
:
testeSingleValueMatrix = cell2mat(teste)
%// 500x500 logical matrix
Now, by including a bsxfun
command in a single cellfun
, we can boil down the above to one single line; the condensed solution given in (+)
above, namely
teste = cell2mat(cellfun(@(x) bsxfun(@ge, x, mode(x(:))), c, 'UniformOutput', false));
%// 500x500 logical matrix
You can make use of cellfun
command
myCellMatrix = ... %// 5x5 cell matrix with 100x100 data entries
modes = cellfun(@(x) mode(x(:)), myCellMatrix);
Output will be a 5x5
value matrix containing the mode for each 100x100 data set.
Upvotes: 4