How to store values in a array from a loop?

3 次查看(过去 30 天)
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by