MM = [3 3 3; 3 3 3; 0 0 0;0 0 0;3 3 3;3 3 3;0 0 0;0 0 0;0 0 0;3 3 0;0 3 3;0 0 3;3 0 0;3 3 0;3 3 3;0 0 0; 0 0 0;3 3 3;0 0 0];
i want to get output matrix in this shape:
output=[3 3 3;3 3 3;3 3 3;3 3 3 ;3 3 3;3 3 3;4 4 4;4 4 4;4 4 4;3 3 4;4 3 3;4 4 3;3 4 4;3 3 4;3 3 3;3 3 3;3 3 3;3 3 3;0 0 0];
% --------------------------------------------------------------
% This coding is working nicely on a column vector M.
% How to apply it on a matrix MM as given above.
%--------------------------------------------------------------
M =[3,3,0,0,3,3,0,0,0,3,3,0,0,3,3,0,0,3,0]';m = M;m(m == 0) = nan;%lo - define the indices of the values that need to be changed:
lo = fillmissing(m,'previous') == 3 & fillmissing(m,'next') == 3 & isnan(m); % ii - assign numbers to intervals:
ii = cumsum([0;diff(lo)>0]).*lo; %{
p - count the amount of each value of vector ii:1 element - the number of zeros (0's);2 element the number of values in the first interval.3 element the number of values in the second interval, etc.:%}
p = accumarray(ii(:)+1,1);% Let a - the values for to be changed (vector with length equal max(ii), in our case - 4):
a = [3,4,4,3]'; % HERE fixed..
% Condition for intervals with length equal 1
a(p == 1) = 3;% Replacement:
M(lo) = a(ii(lo));
Thanks for all cooperation.
Very thankful for support in advance.
Best Answer