How to store values in a array from a loop?

3 ビュー (過去 30 日間)
Agustin
Agustin 2017 年 4 月 13 日
コメント済み: Agustin 2017 年 4 月 14 日
Hi; I have the following code:
Y = imread('img14g.tif');
X = imread('img14bl.tif');
Samples_Y = Y(1:20:512, 1:20:768);
Samples_X = X(1:20:512, 1:20:768);
N = size(Samples_X,1) * size(Samples_X,2); % Number of pixels
Z = zeros(N,7);
for i = 2:25
for j = 2:38
for m = 1:N
Z(m,:)=[Samples_X(i, j), Samples_X(i, j+1),Samples_X(i,j-1), ...
Samples_X(i-1,j+1),Samples_X(i+1,j+1),Samples_X(i-1,j-1),Samples_X(i+1,j-1)];
end
end
end
I need to store the set of pixels in Z but it only stores the last set of pixels for (25,38) on each row of Z. Can somebody help me with this?

採用された回答

dpb
dpb 2017 年 4 月 13 日
...
m=0;
for i = 2:25
for j = 2:38
m=m+1;
Z(m,:)=[Samples_X(i, j), Samples_X(i, j+1),Samples_X(i,j-1), ...
Samples_X(i-1,j+1),Samples_X(i+1,j+1),Samples_X(i-1,j-1),Samples_X(i+1,j-1)];
end
end
Your loop is storing the same value in all rows every pass thru.
NB: length(2:25)=24 and length(2:38)=37 whereas N=26*39 so your end array is smaller than N by 888 vis a vis 1014 for whatever that matters.
  1 件のコメント
Agustin
Agustin 2017 年 4 月 14 日
Thanks! It worked.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by