# How to use linear interpolation for filling with 3s inside empty spaces in a matrix of os and 3s

조회 수: 2 (최근 30일)
M.S. Khan . 2019년 7월 26일
댓글: M.S. Khan . 2019년 7월 30일
M = [0 0 0;... 0 0 3;... 3 3 0;... 0 3 3;... 3 0 0;... 0 0 3;... 3 0 0;... 0 0 0]; I want to use interpolation to fill the gaps between 3s. I tried different methods but no satisfactory answer Is there any other method possible to apply plz Thanks for all cooperation
##### 댓글 수: 2표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
M.S. Khan 2019년 7월 27일
편집: dpb 님. 2019년 7월 27일
M =[
0 0 0
0 0 3
0 3 0
3 0 3
0 3 0
0 0 3
3 0 0
0 0 0]
in this M matrix, using interpolation i want to fill gaps with 3s between any 3s. for example, in first column, first 3 is in the 4th row and last 3 is in the 7th row. i want to fill rows 5 and 6 also with 3s. same for other columns.
Thanks and regards for all cooperation

댓글을 달려면 로그인하십시오.

### 답변 (3개)

Matt J 2019년 7월 27일
편집: Matt J 님. 2019년 7월 27일
One way:
result = sqrt(cummax(M,1).*cummax(M,1,'reverse'))
##### 댓글 수: 2표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
Matt J 2019년 7월 27일
My answer addresses your posted question. I suggest you Accept-click one of the answers given to you and then post a new question that covers your more complicated M.

댓글을 달려면 로그인하십시오.

dpb 2019년 7월 27일
for i=1:size(M,2)
ix=find(M(:,i)==3);
if numel(ix)>1
M(ix(1):ix(end),i)=3;
end
end
##### 댓글 수: 2표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
dpb 2019년 7월 27일
편집: dpb 님. 2019년 7월 27일
LOL! I knew that was coming while writing the above...illustrates that over-simplification gets the right answer to the wrong question.
How large are your arrays and what are actual values in real application? Such pattern matching may well be better suited to casting the values to char() as then can search for string match as patterns...

댓글을 달려면 로그인하십시오.

Andrei Bobrov 2019년 7월 27일
편집: Andrei Bobrov 님. 2019년 7월 30일
s = size(M);
[a,b] = regexp(join(string(M)',''),'30+3');
jj = repelem(1:s(2),cellfun(@numel,a));
lo = zeros(s);
lo(sub2ind(s,[a{:}]+1,jj)) = 1;
lo(sub2ind(s,[b{:}],jj)) = -1;
M(cumsum(lo)>0) = 3;
Other variant:
M = [0 0 0; 2 2 3; 3 3 0; 0 0 0; 3 3 0; 2 2 3; 0 0 0; 3 3 2; 0 0 0; 3 3 3];
m = M;
m(m == 0) = nan;
M(fillmissing(m,'previous') == 3 & fillmissing(m,'next') == 3) = 3;
##### 댓글 수: 1표시 없음숨기기 없음
M.S. Khan 2019년 7월 30일
% Mr. Andrei, can you help me to sort out such problems.
% 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

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!