how to fill gaps of rows with 3s of a matrix under certain conditions.

I have a matrix M.
M =
0 0 3
3 0 0
0 3 0
0 0 0
3 0 3
0 3 0
3 0 0
0 0 0
0 3 3
lets take first column, in the 2nd row is first 3 and next occurence of 3 is in the 4th row. so all should be filled with 3s inside 2nd and 4th row.
it will be like this:
3
3
3
3
then in the same first column, occurence of next 3 should be watached. its 6th row and 8th row.
if there is one zeros among 3's according to rows, it should be filled with 3. if more than one zeros among 3s with respect to rows, that rows of zeros should be filled or replaced with 4.
if its like: 3 % there is only one zeros among rows of 3s
0
3
so it should look like:
3
3
3
i want first column like this:
0
3
3
3
3
3
3
0
0
in 2nd column, first occurence of 3 is in 3rd row and next occurence of 3 is on 6th row.
it will be like this:
3
3
3
3
in the same 2nd column, next occurence of 3 is on 6th rows and next one is 9th rows.
so in next occurence, if 0s (zeros ) between 3's are more than one. it should be filled with 4.
2nd column should look like this:
0
0
3
3
3
3
4
4
3
your cooperation is highely appreciated.

 Accepted Answer

Let
M = [0 0 3
3 0 0
0 3 0
0 0 0
3 0 3
0 3 0
3 0 0
0 0 0
2 3 3
0 0 0
3 0 0
0 1 0
0 0 0
3 3 3
0 0 0];
[r,c] = size(M);
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, own numbering for each column:
ii = cumsum([zeros(1,c);diff(lo)>0]).*lo;
[~,jj] = ndgrid(1:r,1:c);
%{
p - count the amount of each value of matrix ii for each column
1 line the number of zeros (0's)
2 line the number of values in the first interval.
3 line the number of values in the second interval, etc.:
%}
p = accumarray([ii(:)+1,jj(:)],1);
% discard information about zeros:
p = p(2:end,:);
% a - enter the values by which our intervals will be filled (first - 3, next 4):
a = repmat([3;4*ones(max(ii(:))-1,1)],1,c);
% condition for intervals with a single value:
a(2:end,:) = a(2:end,:) - (p(2:end,:) == 1);
% replace the values in the M matrix with the values from a:
M(lo) = repelem(a(:),p(:));

9 Comments

Thanks Dr. Andrei Bobrov for your feedback
could you explain to me this coding.. how does it work.
Thanks Bro. Very thankful for your professional tips. Wish you all happiness and success in your life. could you plz explain to me the coding in an easy way. warm regards
Hi Mohammad!
I'm added comments in code.
could we repeat the same coding if we are changing the positions of 3 inside different rows.
Lets suppose i have a Matrix M.
M is a small matrix. i am working on Big Matrix. My focus is on central (Middle) column. Using mid column, i want to fill (repalce '0' with '3' & '4') according to their positions in the mid colums with to respect to all rows.
M =
0 3 0
0 3 0
3 0 0
0 0 0
0 3 0
3 3 3
0 0 0
0 0 0
0 0 0
0 3 0
3 3 0
0 0 0
3 0 0
0 3 0
0 3 3
0 0 0
0 0 0
0 3 0
0 0 0
---------------------------------
% in first occurence of 3 and next occurence of 3 should be filled with 3s. carry on the same mid column, in next occurence from first to next one (6th to 10th rows)should be filled with 4s. Continue by same column, 13 th and 14th rows should be filled as 4 while next occurence of 3 and next 3s gaps( 17th to 19th rows) should be filled with 3
% I want to get the 'required colum' where 0's are replaced with bold numbers 3s and 4s with respect to their positions of rows in the mid colum of Matrix M.
% Your help and cooperation will be highly appreacitead. Regards in advance.
---------------------------------------------------------
Mid column Required
3 3
3 3
0 3
0 3
3 3
3 3
0 4
0 4
0 4
0 4
3 3
3 3
0 4
0 4
3 3
3 3
0 3
0 3
0 3
3 3
0 0
First and last intervals between 3's need be filled - 3's, other intervals - 4's?
See also: https://uk.mathworks.com/matlabcentral/answers/474418 where the OP asked the same question (and is equally unclear as to the criteria for 3 or 4, which apparently can also be 5, 6 or anything else!).

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!