필터 지우기
필터 지우기

replace numbers of margins of metrix( how t0 fix it ?)

조회 수: 1 (최근 30일)
Talat
Talat 2011년 4월 10일
i want to replace margins of matrix by another number by using 'for loop'
{im=[0 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1;
0 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1;
0 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1;
0 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1;
0 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1;
0 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1];
[r c]= size(im);
for i=1:size(im(1,end,:))
for j=1:size(im,2)
im(i, j)=3;
end
end
}
it only return first row by replacing with '3'.it should at least return first and last row with '3'.... but it doesn't. how do i write a code to replace all margins(first_row, first_column, last_row and last_column) by '3' using 'for loop'....

채택된 답변

Walter Roberson
Walter Roberson 2011년 4월 10일
im is a 2 dimensional array, so in your expression im(1,end,:) the : is going to just reference the entire 2D array, leaving the expression equivalent to im(1,end) . im(1,end) refers, though, to im(1,size(im,2)) which is the single element that is the top right corner of the array. You then take size(im(1,end,:)) so that size() is going to be referring to size() of something that is 1x1 and so "i" is going to refer only to the first row.
  댓글 수: 2
Talat
Talat 2011년 4월 10일
isn't it possible to use for loop "for addressing marginal areas".... cz there are other ways to fix this prob, but i wana to fix it through 'for loop'....and em still not getting the way
Walter Roberson
Walter Roberson 2011년 4월 10일
Sure it is possible to use for loops for what you are doing: what I pointed out is the part your code fails at. Think more closely about what it is you want to take the size() of.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2011년 4월 10일
[m,n]=size(im);for ii = 1:m,for jj = 1:n,if ii == 1 | ii == m | jj == 1 | jj == n, im(ii,jj) = 3; end;end;end

Andrei Bobrov
Andrei Bobrov 2011년 4월 10일
im([true(1,n);repmat([true false(1,n-2) true],m-2,1);true(1,n)])=3

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by