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

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