How to store values in a array from a loop?
조회 수: 4 (최근 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?
댓글 수: 0
채택된 답변
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 Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!