Clear Filters
Clear Filters

How to store values in a array from a loop?

3 views (last 30 days)
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?

Accepted Answer

dpb
dpb on 13 Apr 2017
...
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.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!