Filling missing elements in a matrix.

조회 수: 4 (최근 30일)
Sridutt Gokul
Sridutt Gokul 2019년 12월 3일
댓글: Adam Danz 2019년 12월 3일
Hello,
I have a matrix like this:
A = [1 0 2 0 0 2 2 2 0 2 2 2 [] [] [] [] [] ;
0 3 2 1 0 1 2 0 1 0 0 0 [] [] [] [] [] ;
0 2 3 0 2 0 1 3 0 0 0 0 [] [] [] [] [] ;
0 0 2 2 0 1 1 0 2 0 0 0 [] [] [] [] [] ;
3 0 3 0 0 0 0 1 0 1 0 1 [] [] [] [] [] ;
4 0 4 0 0 1 1 0 0 0 0 1 0 [] [] [] [] ;
0 0 3 0 0 0 0 0 0 0 0 0 0 [] [] [] [] ;
0 0 2 3 0 0 0 0 0 0 0 0 0 2 [] [] [] ;
0 0 0 3 0 1 0 0 0 0 0 [] 1 0 [] [] [] ;
4 0 0 0 3 3 1 0 0 [] 0 [] 0 0 [] 0 0 ;
0 0 0 0 2 5 1 0 0 0 0 0 0 0 [] 0 0 ;
0 0 0 0 0 1 0 0 0 0 0 0 0 0 [] 0 0 ;
0 0 2 [] 1 3 0 0 0 0 0 0 0 0 [] 0 0 ;
2 2 2 3 0 0 0 0 0 0 0 0 0 0 [] 0 0 ;
0 1 1 2 1 0 [] 0 [] 0 [] 0 [] 0 [] 0 0 ;
0 0 0 [] 0 0 0 [] 0 [] 0 [] [] 0 [] 0 0 ;
1 0 1 [] 0 0 0 0 [] 0 [] 0 [] 0 [] 0 0 ;
0 1 0 0 0 0 0 [] 0 [] 0 [] [] 0 [] 0 0 ;
[] 3 0 1 0 0 [] 0 [] 0 [] 0 0 0 [] 0 [] ;
[] [] [] [] 0 0 0 [] 0 [] 0 [] 0 0 [] 0 [] ;
[] [] [] [] 0 [] [] [] [] [] [] [] [] [] [] [] [] ;
[] [] [] 0 [] 0 0 0 [] 0 [] 0 0 0 0 [] [] ;
[] [] [] [] [] 0 0 [] 0 [] 0 [] 0 0 0 [] [] ;
[] [] [] [] 0 0 [] 0 [] 0 [] 0 0 0 [] [] [] ];
The [] denote missing elements. I want to fill in all the missing elements by holding the previous value in the same row (value to the left of the missing element) and in cases where the row begins with [] it should fill in the values from the right, again in the same row.
Please help me, I've tried the fillmissing function but it does not produce the results I am looking for.
Thanks in advance.
  댓글 수: 1
Adam Danz
Adam Danz 2019년 12월 3일
편집: Adam Danz 2019년 12월 3일
That doesn't describe your real variable 'A'. In your description, A is a matrix but matrices cannot have empty values. They can have NaN values, but not empty values.
I'm guessing A is actually a cell array of scalar values that looks like this (execute this in the command window and please confirm this assumption).
A = num2cell(randi(9,20,10)); % random integers
A(randi(numel(A),1,100)) = {[]}; % remove 100 values and replace with empty
Also, what if the preceeding value (or the next value) is also empty?

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

채택된 답변

Adam Danz
Adam Danz 2019년 12월 3일
편집: Adam Danz 2019년 12월 3일
Use F = fillmissing(A,method); requires >=r2016b
Here's a demo
% Create a 20x10 cell array of integer values
% and replace 100 of the values with empties
A = num2cell(randi(9,20,10));
A(randi(numel(A),1,100)) = {[]};
% Fill empties with NaNs
A(cellfun(@isempty,A)) = {NaN};
% Convert to matrix and replace all empties with
% previous value except rows that lead with empty
F = fillmissing(cell2mat(A).','previous').';
% Replace the leftover rows the lead with empties
F = fillmissing(F.','next').';
Alternatively you could try this but in my quick tests, it didn't work so well
F = fillmissing(cell2mat(A).','previous','EndValues','next').';
If you need to convert the matrix back to a cell array (not recommended)
C = num2cell(F);
  댓글 수: 2
Sridutt Gokul
Sridutt Gokul 2019년 12월 3일
Thank you, this is what I needed.
Adam Danz
Adam Danz 2019년 12월 3일
Glad I could help!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by