add elements to a matrix

I want to define a 20*100 matrix and add elements to it row by row. I divide a pitcture to 20 blocks containing 15360 pixels and get average value of H of the blocks. The code is shown below:
Hsv = rgb2hsv(mov);
H = Hsv(:, :, 1);
H = reshape(H, 120, 4, 128, 5);
blockMean = 255*reshape(sum(sum(H, 1), 3), 4, 5) / 15360;
blockArea = reshape(blockMean, 1, 20);
Now I need to get blockArea of 100 pictures and add them into a 20*100 matrix. How can I achieve it?

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 3 日

1 投票

If your are asking about vertical concatenation
out=[]
for k=1:10
A=rand(5,5)
out=[out;A]
end

6 件のコメント

Xiaochao
Xiaochao 2012 年 10 月 4 日
Thanks for your help. It does work. But I have a problem that the order of the element blockArea is from 100th picture to 1st picture. How can I make it be from 1st picture to 100th picture.
Walter Roberson
Walter Roberson 2012 年 10 月 4 日
See fliplr() and flipud()
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 4 日
編集済み: Azzi Abdelmalek 2012 年 10 月 4 日
or try this (switch out=[out;A] to out=[A;out])
out=[]
for k=1:10
A=rand(5,5)
out=[A;out]
end
Jan
Jan 2012 年 10 月 4 日
Pre-allocate!
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 4 日
Then the code will change
m=10;n=5
out=zeros(m*n,n)
for k=1:m
A=rand(n)
out(m*n-k*5+1:m*n-(k-1)*n,:)=A;
end
Xiaochao
Xiaochao 2012 年 10 月 4 日
thank you very much. I learn a lot from you guys.

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

Jan
Jan 2012 年 10 月 4 日

0 投票

out = zeros(100, 20); % Pre-allocation
for k = 1:10
A = rand(1, 20);
out(k, :) = A;
end
Pre-allocation is essential. When the array grow in each iteration, you do not allocate for 100*20*8 bytes, but:
sum(1:100) * 20 * 8 bytes
And these are 808kB already.

1 件のコメント

Xiaochao
Xiaochao 2012 年 10 月 4 日
Thanks, it works well.

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

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

タグ

質問済み:

2012 年 10 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by