Sum of a cell of arrays
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
My code consists of scanning the current directory for all .bmp image files, creating a cell array (called "C") with the names of these files, and then imread-ing them. This creates a cell of the array images.
My question is how to take the sum of each image array read to create a single new array? The sum function obviously doesn't work since it produces a single value output. Manually typing C{1} + C{2} (and so on) will get me the correct results, but I need this to be automated and to work for any number of image files. I have tried to do a recursive function as well, but apparently it doesn't like adding arrays together. My code is shown below:
files = dir(fullfile(pwd, '*.bmp'));
C = cell(length(files), 1);
for k = 1:length(files);
names = files(k).name;
C{k} = imread(names);
end
le = length(files);
Final = recursive(le);
function r = recursive(x)
if (x == 0)
total = 0;
else
files = dir(fullfile(pwd, '*.bmp'));
C = cell(length(files), 1);
for k = 1:length(files);
names = files(k).name;
C{k} = imread(names);
total = C{x} + recursive(x-1);
end
end
r = total;
I of course have the first set of scripts under another function, but have not pasted it here. Any help would be appreciated.
0 comentarios
Respuestas (2)
José-Luis
el 23 de Jun. de 2014
%dummy data
C = cell(10,1);
m = 15; %image size
n = 18;
numImages = 20;
for ii = 1:numImages
C(ii) = {rand(m,n)};
end
%The sum:
your_sum = sum ( reshape (cell2mat(C),m,n,numImages), 3 );
4 comentarios
José-Luis
el 24 de Jun. de 2014
And how would you define "whiteness" mathematically. Maybe you need to make your image grayscale first and pick the ones with the lowest sum.
Michael sch
el 23 de Jun. de 2014
so what you can do get length(x) and then loop for all x and then sum them k=1:length(x) y=y+x{k} end
x{1}=[1 2; 3 4]
x{2}=[ 5 6 ;7 8]
x{1}+x{2}
ans =
6 8
10 12
length(x)
ans =
2
0 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!