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

1 Ansicht (letzte 30 Tage)
Talat
Talat am 10 Apr. 2011
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'....

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 10 Apr. 2011
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 Kommentare
Talat
Talat am 10 Apr. 2011
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 am 10 Apr. 2011
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Andrei Bobrov
Andrei Bobrov am 10 Apr. 2011
[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 am 10 Apr. 2011
im([true(1,n);repmat([true false(1,n-2) true],m-2,1);true(1,n)])=3

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by