Concatenate the produced arrays from a for loop?

4 ビュー (過去 30 日間)
iontrap
iontrap 2020 年 8 月 14 日
コメント済み: Stephen23 2020 年 8 月 22 日
I am trying to calculate the mean and standard deviation of the R,G,B channels of 50 image files in sequence. I would like to write out the mean of these into a single column vector per color. This would result in 3 50x1 column vectors - one each for R, G, B. I am able to calculate the means, but they are being output separately. Thanks
for j = 1:50
fname = sprintf('test (%d).bmp',j);
image4d = imread(fname);
Mean = squeeze(mean(image4d, [1 2]));
MeanR = Mean(1,:)
MeanG = Mean(2,:)
MeanB = Mean(3,:)
end
My output looks like:
MeanR =
25.1629
MeanG =
3.1821
MeanB =
0
MeanR =
31.6650
MeanG =
3.6093
MeanB =
0
.
.
For all 50 images.
  2 件のコメント
Sara Boznik
Sara Boznik 2020 年 8 月 14 日
Maybe you can add one for loop and define k=0 and in each loop is k=k+1, and after you write the
MeanR(k,:) = Mean(1,:)
I am not sure but you can try it.
Wish you best of luck.
iontrap
iontrap 2020 年 8 月 14 日
Unfortunately, this yields a 50x1 column of duplicate results for one image.

サインインしてコメントする。

採用された回答

Matt J
Matt J 2020 年 8 月 15 日
編集済み: Matt J 2020 年 8 月 15 日
Mean=nan(50,3);
for j = 1:50
fname = sprintf('test (%d).bmp',j);
image4d = imread(fname);
Mean(j,:) = mean( image4d , [1 2]) ;
end
MeanR=Mean(:,1);
MeanG=Mean(:,2);
MeanB=Mean(:,3);
  3 件のコメント
Matt J
Matt J 2020 年 8 月 15 日
It just pre-allocates an array of NaNs so that you have somewhere to put the data generated by the loop, e.g.,
>> nan(5,3)
ans =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
Stephen23
Stephen23 2020 年 8 月 22 日
"Could you explain the purpose of this nan(50,3)?"

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by