Creating an array with variables or a loop

조회 수: 1 (최근 30일)
Karina
Karina 2014년 6월 26일
댓글: Karina 2014년 6월 27일
I have the next code:
for k=1:10
imDs{k}=imread([num2str(k),'.png']);
iGrisB{k} = imDs{k}(:, :, 3);
%Datos
media{k} = mean(iGrisB{k}(:));
Std{k}=std(double(iGrisB{k}(:)));
DMA{k}=mad(double(iGrisB{k}(:)));
MIN{k}=min(iGrisB{k}(:));
MAX{k}=max(iGrisB{k}(:));
Mode{k}=mode(double(iGrisB{k}(:)));
end
dat={media{1},Std{1},DMA{1},MIN{1},MAX{1},Mode{1}
media{2},Std{2},DMA{2},MIN{2},MAX{2},Mode{2}
media{3},Std{3},DMA{3},MIN{3},MAX{3},Mode{3}
media{4},Std{4},DMA{4},MIN{4},MAX{4},Mode{4}
media{5},Std{5},DMA{5},MIN{5},MAX{5},Mode{5}
media{6},Std{6},DMA{6},MIN{6},MAX{6},Mode{6}
media{7},Std{7},DMA{7},MIN{7},MAX{7},Mode{7}
media{8},Std{8},DMA{8},MIN{8},MAX{8},Mode{8}
media{9},Std{9},DMA{9},MIN{9},MAX{9},Mode{9}
media{10},Std{10},DMA{10},MIN{10},MAX{10},Mode{10}};
t=uitable;
ColNames={'Media','DS', 'DMA','Min','Max','Mode'};
set(t,'Data',dat,'ColumnName',ColNames, ...
'Position', [40 225 525 400])
My problem is that I want to find an easier way to do the part of dat={media{1},Std{1}...media{10},Std{10}} because they are not only 10 images, I need to process almost 60 images. Also it has to be in a flexible way, because not always I have to process 60 images sometimes less or more. Thank you. Regards.

채택된 답변

Jan
Jan 2014년 6월 26일
편집: Jan 2014년 6월 26일
You can create the output directly:
n = 10;
dat = cell(n, 6);
for k = 1:n
imDs = imread([num2str(k),'.png']);
iGrisB = imDs(:, :, 3);
B = double(iGrisB(:));
dat(k, :) = {mean(B), std(B), mad(B), min(B), max(B), mode(B)};
end
Another option is the original loop an this for the joining:
dat = cat(1, media, Std, DMA, MIN, MAX, Mode).';
  댓글 수: 1
Karina
Karina 2014년 6월 27일
Thank you it functions very well.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ben11
Ben11 2014년 6월 26일
You can move the dat(...) inside your for-loop:
dat =cell(NumFiles,6); % NumFiles is the # of images you have.
for k=1:NumFiles
imDs{k}=imread([num2str(k),'.png']);
iGrisB{k} = imDs{k}(:, :, 3);
%Datos
media{k} = mean(iGrisB{k}(:));
Std{k}=std(double(iGrisB{k}(:)));
DMA{k}=mad(double(iGrisB{k}(:)));
MIN{k}=min(iGrisB{k}(:));
MAX{k}=max(iGrisB{k}(:));
Mode{k}=mode(double(iGrisB{k}(:)));
dat(k,:) = {media{k} Std{k} DMA{k} MIN{k} MAX{k} Mode{k}};
end
t=uitable;
ColNames={'Media','DS', 'DMA','Min','Max','Mode'};
set(t,'Data',dat,'ColumnName',ColNames, ...
'Position', [40 225 525 400])

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by