フィルターのクリア

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
  1 件のコメント
Talat
Talat 2011 年 4 月 11 日
thank you soooooooo much....

サインインしてコメントする。


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 Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by