replace numbers of margins of metrix( how t0 fix it ?)
1 次查看(过去 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'....
0 个评论
采纳的回答
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 个评论
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
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!