How to manipulate a matrix under certain conditions to fills gaps with numbers.

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

7 Comments

What determines if the zeros should be replaced by threes or replaced by fours?
not necessary, we can change zeros by any other numbers. like 5 and 6.
I think I was not clear, so let me try to rephrase my question.
How do you know which number should be used to replace the zeros at a particular location. Is this determined by, for instance, the first and/or last column? Or the order of the gaps?
In your example, you've replaced the first lot of 0s by 3, the 2nd lot by 4, the 3rd lot by 4, the 4th lot by 3 and the final 0 by nothing. As Jos says, there's no clear logic as to why 3 or 4 was chosen.
Oh, and please don't ask the same thing twice in different posts. This just wastes our effort, with different people asking the same question twice.
Sorry Mr. Guillaume. Due to Lack of experience in use of community plateform.
In my matrix, i need values like required colum.
Mr. Jos and Guillaume, i am trying to replace 0s among first 3 and then next 3 by 3s.
simillarly in next group , the zeros between first 3 and next 3 are replaced by 4.
Thanks for reply and cooperation. warm regards

Sign in to comment.

 Accepted Answer

Let MM - your 'required colum'.
MM =[3,3,0,0,3,3,0,0,0,3,3,0,0,3,3,0,0,3,0]';
m = MM;
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:
MM(lo) = a(ii(lo));

12 Comments

Dear friends, i mean, in MM matrix:
for example,
in first batch of 0s among first 3 and last 3 i.e [ 3,3,0,0,3] ==> [ 3,3,3,3,3]
in 2nd batch of 0s among first 3 and last 3 i.e 3,0,0,0,3 ==>3,4,4,4,3
in 3rd in 2nd batch of 0s among first 3 and last 3 i.e 3,0,0,3 ==> 3,4,4,3
in 3rd in 2nd batch of 0s among first 3 and last 3 i.e 3,0,0,3,0 ==> 3,3,3,3,0
Great! Dr. Andrei Bobrov, if i want to apply it on matrix M as given above. How will it work?
MM =[3,3,0,0,3,3,0,0,0,3,3,0,0,3,3,0,0,3,0]' is an column vector. i want to apply on
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]
What is the order of filling intervals for columns whose number of intervals is not equal to 4.
lets M be the matrix with same pattern of 3's and 4s as the mid column.
M = [0 3 0
0 3 0
3 0 3
0 0 0
0 3 0
0 3 3
3 0 0
0 0 0
0 0 0
0 3 0
3 3 3
0 0 3
3 0 0
0 3 0
0 3 3
3 0 0
0 0 3
0 3 0
0 0 0]
You applied it one colum but i want it to apply on matrix.
Warm regards for all cooperation,guidance and high class feedback.
Which fill pattern to use for columns, where the number of intervals will be different from 4?
Now for middle column - [3;4;4;3] .
On example for follow case:
M = [0 3 0
3 3 0
0 0 3
0 0 0
0 3 0
3 3 3
0 0 0
0 0 0
0 0 0
3 3 0
0 3 3
0 0 3
3 0 0
0 3 0
0 3 0
3 0 0
0 0 3
0 3 0
3 0 0]
Sir, if we use the same pattern as mid column. How will we apply it on matrix?
Lets suppose this the fill pattern as you are asking as i understood.
M = [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
0 3 0
3 3 3
0 0 0
0 0 0
3 3 3
3 0 3]
Bro Andre, you have applied the above codr on vector. I want to apply the same code on on matrix M. How can i apply the same coding on the matrix M as I given above.plz need your help. Warm regards
I have this matrix M. i applied following code so many times but i am not getting the result.
M = [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];
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));
i want to get result in this form:
M = [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];
Cooperation and guidance is highly appreciated. Warm regrads
Dr. Andrei Bobrov, Plz waiting for your professional reply. Thanks for all your professional cooperation and guidance. God bless you, sir.

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!